码迷,mamicode.com
首页 > 系统相关 > 详细

Linux环境编程之IPC进程间通信(三):FIFO

时间:2014-06-20 12:22:33      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:ipc   fifo   

管道是没有名字的,因此它只能在有亲缘关系的进程间使用,给管道加上名字,我们称之为有名管道FIFO,当然FIFO与管道之间不止有没有名字的区别,还有其他区别下面会提到。与管道类似的是,FIFO是一个单向(半双工)数据流。不同于管道的是,每个FIFO有一个路径名与之关联,从而允许无亲缘关系的进程之间访问同一个FIFO。FIFO是一种文件类型。stat结构成员st_mode的编码指明文件是否是FIFO类型,可以用S_ISFIFO宏对此进行测试。

FIFO由mkfifo函数创建,它已经隐含指定了O_CREAT|O_EXCL。也就是说,如果FIFO存在就返回一个EEXIST错误,如果不存在就创建一个新的FIFO。FIFO由open打开而不是mkfifo。因此要打开一个已存在的FIFO或创建一个新的FIFO,应先调用mkfifo,再检查它是否返回EEXIST错误,若返回该错误则改为调用open。

<span style="font-size:14px;">#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);</span>
在创建出一个FIFO后,它必须是或者打开来读,或者打开来写,可由open或fopen打开函数。FIFO不能打开来既读又写,因为它是半双工的。

FIFO有下面两种用途:

1、FIFO由shell命令使用以便将数据从一条管道线传送到另一条,为此无需创建中间临时文件。FIFO可被用于复制串行管道命令之间的输出流,于是也就不需要写数据到中间磁盘文件中。如:

<span style="font-size:14px;">mkfifo fifo1
prog3 < fifo1 &
prog1 < infile | tee fifof1 | prog2</span>

创建FIFO,然后在后台启动prog3,它从FIFO读数据。然后启动prog1,用tee将输出发送到FIFO和prog2。图解如下:

bubuko.com,布布扣

图 使用FIFO和tee将一个数据发送到两个进程

2、FIFO用于客户进程-服务器进程应用程序中,以在客户进程和服务器进程之间传递数据。对于客户-服务器程序,改用两个FIFO代替两个管道的代码如下:

mainfifo.c

<span style="font-size:14px;">#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include "fifo.h"

#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define FILE_MODE 	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

extern void client(int, int);
extern void server(int, int);

int
main(int argc, char **argv)
{
	int		readfd, writefd;
	pid_t	pid;

	/*creat tow FIFOs; OK if they already exist*/
	if((mkfifo(FIFO1, FILE_MODE) < 0) && (errno = EEXIST))
		printf("can't creat %s.\n", FIFO1);
	if((mkfifo(FIFO2, FILE_MODE) < 0) && (errno = EEXIST)){
		unlink(FIFO1);
		printf("can't creat %s.\n", FIFO2);
	}

	if((pid = fork()) < 0){
		printf("can't fork.\n");	
		return -1;
	}else if(pid == 0){			/*child*/
		readfd  = open(FIFO1, O_RDONLY, 0);
		writefd = open(FIFO2, O_WRONLY, 0);

		server(readfd, writefd);
		exit(0);
	}
	/*parent*/
	writefd = open(FIFO1, O_WRONLY, 0);
	readfd  = open(FIFO2, O_RDONLY, 0);

	client(readfd, writefd);

	waitpid(pid, NULL, 0);			/*wait for child to terminate*/

	close(readfd);
	close(writefd);

	unlink(FIFO1);
	unlink(FIFO2);
	exit(0);
}</span>
client.c

<span style="font-size:14px;">#include "fifo.h"

int
client(int readfd, int writefd)
{
	size_t 	len;
	ssize_t	n;
	char 	buff[MAXLINE];

	/*read pathname*/
	fgets(buff, MAXLINE, stdin);
	len = strlen(buff);
	if(buff[len-1] == '\n')
		len--;

	/*write pathname to IPC channel*/
	write(writefd, buff, len);

	/*read from IPC, write to standard output*/
	while((n = read(readfd, buff, MAXLINE)) > 0)
		write(STDOUT_FILENO, buff, n);
}</span>
server.c

<span style="font-size:14px;">#include "fifo.h"

void 
server(int readfd, int writefd)
{
	int		fd;
	ssize_t	n;
	char 	buff[MAXLINE+1];

	/*read pathname frome IPC channel*/
	if((n = read(readfd, buff, MAXLINE)) == 0){
		printf("end-of-file while reading pathname.\n");
		return ;
	}
	buff[n] = '\0';			/*null terminate pathname*/

	if((fd = open(buff, O_RDONLY)) < 0){
		/*error:must tell client*/	
		snprintf(buff+n, sizeof(buff)-n, ":can't open, %s\n", strerror(errno));
		n = strlen(buff);
		write(writefd, buff, n);
	}else{
		/*open succeeded:copy file to IPC channel*/	
		while((n = read(fd, buff, MAXLINE)) > 0)
			write(writefd, buff, n);
		close(fd);
	}

}</span>
fifo.h

<span style="font-size:14px;">#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
</span>
Makefile

<span style="font-size:14px;">all:mainfifo 
mainfifo:mainfifo.c server.c client.c
	gcc  server.c client.c mainfifo.c -o mainfifo</span>
图解上面的程序如下:

bubuko.com,布布扣
图 使用两个FIFO的客户-服务器例子
上面的例子送虽然使用了FIFO,但仍然是有亲缘关系的进程间通信,下面给出无亲缘关系的客户和服务器的例子。
客户程序client_main.c如下:
<span style="font-size:14px;">#include "fifo.h"

extern client(int, int);

int
main(int argc, char **argv)
{
	int 	readfd, writefd;

	writefd = open(FIFO1, O_WRONLY, 0);
	readfd  = open(FIFO2, O_RDONLY, 0);

	client(readfd, writefd);

	close(readfd);
	close(writefd);

	unlink(FIFO1);
	unlink(FIFO2);
	exit(0);
}</span>
client.c
<span style="font-size:14px;">#include "fifo.h"

int
client(int readfd, int writefd)
{
	size_t 	len;
	ssize_t	n;
	char 	buff[MAXLINE];

	/*read pathname*/
	fgets(buff, MAXLINE, stdin);
	len = strlen(buff);
	if(buff[len-1] == '\n')
		len--;

	/*write pathname to IPC channel*/
	write(writefd, buff, len);

	/*read from IPC, write to standard output*/
	while((n = read(readfd, buff, MAXLINE)) > 0)
		write(STDOUT_FILENO, buff, n);
}</span>
服务器程序server_main.c如下:
<span style="font-size:14px;">#include "fifo.h"
#include <sys/stat.h>
#define FILE_MODE 	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

extern void server(int, int);

int
main(int argc, char **argv)
{
	int 	readfd, writefd;

	/*creat two FIFOs:OK if they already exist*/
	if((mkfifo(FIFO1, FILE_MODE) < 0) && (errno != EEXIST)){
		printf("can't create %s.\n", FIFO1);
	}
	if((mkfifo(FIFO1, FILE_MODE) < 0) && (errno != EEXIST)){
		unlink(FIFO1);
	 	printf("can't create %s.\n", FIFO1);
	}

	readfd  = open(FIFO1, O_RDONLY, 0);
	writefd = open(FIFO2, O_WRONLY, 0);

	server(readfd, writefd);
	exit(0);
}</span>
server.c
<span style="font-size:14px;">#include "fifo.h"

void 
server(int readfd, int writefd)
{
	int		fd;
	ssize_t	n;
	char 	buff[MAXLINE+1];

	/*read pathname frome IPC channel*/
	if((n = read(readfd, buff, MAXLINE)) == 0){
		printf("end-of-file while reading pathname.\n");
		return ;
	}
	buff[n] = '\0';			/*null terminate pathname*/

	if((fd = open(buff, O_RDONLY)) < 0){
		/*error:must tell client*/	
		snprintf(buff+n, sizeof(buff)-n, ":can't open, %s\n", strerror(errno));
		n = strlen(buff);
		write(writefd, buff, n);
	}else{
		/*open succeeded:copy file to IPC channel*/	
		while((n = read(fd, buff, MAXLINE)) > 0)
			write(writefd, buff, n);
		close(fd);
	}

}</span>
fifo.h
<span style="font-size:14px;">#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

#define FIFO1 	"/tmp/fifo.1"	
#define FIFO2 	"/tmp/fifo.2"	
#define MAXLINE 4096</span>

参考:《UNIX环境高级编程》、《UNIX网络编程 卷2:进程间通信》










Linux环境编程之IPC进程间通信(三):FIFO,布布扣,bubuko.com

Linux环境编程之IPC进程间通信(三):FIFO

标签:ipc   fifo   

原文地址:http://blog.csdn.net/to_be_it_1/article/details/28384117

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!