FIFO
FIFO又称命名管道,是一种文件类型。通过FIFO,不相关的进程也可以交换数据。
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo( const char *pathname, mode_t mode );
mkfifo函数需要两个参数,第一个参数(pathname)是将要在文件系统中创建的一个专用文件。第二个参数(mode)用来规定FIFO的读写权限。Mkfifo函数如果调用成功的话,返回值为0;如果调用失败返回值为-1。下面我们以一个实例来说明如何使用mkfifo函数建一个fifo,具体代码如下所示:
int ret;
ret = mkfifo( "/tmp/cmd_pipe", S_IFIFO | 0666 );
if (ret == 0) {
// 成功建立命名管道
}
else {
// 创建命名管道失败
}
在这个例子中,利用/tmp目录中的cmd_pipe文件建立了一个命名管道(即fifo)。之后,就可以打开这个文件进行读写操作,并以此进行通信了。命名管道一旦打开,就可以利用典型的输入输出函数从中读取内容。举例来说,下面的代码段向我们展示了如何通过fgets函数来从管道中读取内容:
pfp = fopen( "/tmp/cmd_pipe", "r" );
ret = fgets( buffer, MAX_LINE, pfp );
我们还能向管道中写入内容,下面的代码段向我们展示了利用fprintf函数向管道写入的具体方法:
pfp = fopen( "/tmp/cmd_pipe", "w+ );
ret = fprintf( pfp, "Here’s a test string!/n" );
对命名管道来说,除非写入方主动打开管道的读取端,否则读取方是无法打开命名管道的。Open调用执行后,读取方将被锁住,直到写入方出现为止。一旦用mkfifo创建了一个FIFO,就可以用open打开它。一般的I/O文件函数(close、read、write、unlink等)都可用于FIFO。
若write写一个尚无进程为读而打开的FIFO,则产生信号SIGPIPE,这与管道同。
FIFO的两种用途:
1、 FIFO由shell命令使用以便将数据从一条管道线传到另一条,为此无需创建中间临时文件。
2、 FIFO用于客户进程-服务器进程应用程序中,以在客户程序和服务程序之间传递数据。
示例程序1:进程内部或者有共同祖先进程之间的数据传递
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#define FIFO_SERVER "/home/fifo_internal"
#define MAXLINE 128
main(int argc ,char * argv[])
{
int fd;
FILE *fd1;
char buf_w[100],buf_r[100];
int nwrite,ret,nread;
pid_t pid;
ret = mkfifo( FIFO_SERVER, S_IFIFO | 0666 );
if (ret == 0) {
printf("fifo create successful/r/n");
}
else {
printf("fifo create failure/r/n");
exit(1);
}
fd=open(FIFO_SERVER,O_RDWR|O_NONBLOCK,066);
if(fd==-1)
printf("open error/n");
else
printf("open success/n");
if((pid=fork())<0)
printf("fork error/n");
else if(pid>0){ /*parent*/
if(argc==1)
{
printf("please input your message/n");
fgets(buf_w, MAXLINE, stdin);
}
else
strcpy(buf_w,argv[1]);
printf("/nthe message will be writed to pipe is:/n%s/n",buf_w);
if((nwrite=write(fd,buf_w,MAXLINE))==-1)
{
if(errno==EAGAIN)
printf("write error/n");
else
printf("write %s ok/n",buf_w);
}
else
{
printf("write %s to the FIFO_SERVER/n",buf_w);
}
wait(pid);
}
else if(pid==0){ /*child*/
sleep(3); /*wait parent*/
memset(buf_r,0,sizeof(buf_r));
while((nread=read(fd,buf_r,MAXLINE))>0)
{buf_r[nread]=0;
if(errno==EAGAIN)
printf("no any data yet/n");}
printf("read %s from FIFO/n",buf_r);
close(fd);
unlink(FIFO_SERVER);
}
}
运行:./fifi_internal
[root@localhost yuan]# ./fifo_internal
fifo create successful
open success
please input your message
hello
the message will be writed to pipe is:
hello
write hello
to the FIFO_SERVER
read hello
from FIFO
[root@localhost yuan]#
示例程序2:进程间FIFO读写
file_write.c
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#define FIFO_SERVER "/home/fifoserver9"
#define MAXLINE 128
main(int argc ,char * argv[])
{
int fd;
FILE *fd1;
char buf_w[100],buf_r[100];
int nwrite,ret;
ret = mkfifo( FIFO_SERVER, S_IFIFO | 0666 );
if (ret == 0) {
printf("fifo create successful/r/n");
}
else {
printf("fifo create failure/r/n");
exit(1);
}
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,066);
if(fd==-1)
printf("open error/n");
else printf("open success/n");
if(argc==1){
printf("please input your message/n");
fgets(buf_w, MAXLINE, stdin);
}
else
strcpy(buf_w,argv[1]);
printf("/nthe message will be writed to pipe is: %s/n",buf_w);
if((nwrite=write(fd,buf_w,MAXLINE))==-1)
{
if(errno==EAGAIN)
printf("write error/n");
else
printf("write %s ok/n",buf_w);
}
else
{
printf("write %s to the FIFO_SERVER/n",buf_w);
}
}
file_read.c
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#define MAXLINE 128
main(int argc ,char * argv[])
{
int fd;
char buf_r[100];
int ret,nread;
fd=open(FIFO_SERVER,O_RDONLY|O_NONBLOCK,066);
if(fd==-1)
printf("open error/n");
else
printf("open success/n");
memset(buf_r,0,sizeof(buf_r));
nread=read(fd,buf_r,MAXLINE);
if(errno==EAGAIN)
printf("no any data yet/n");
printf("read %s from FIFO/n",buf_r);
close(fd);
unlink(FIFO_SERVER);
}
运行:
[root@localhost yuan]# ./fifo_write
fifo create successful
open success
please input your message
hello
the message will be writed to pipe is: hello
write hello
to the FIFO_SERVER
[root@localhost yuan]# ./fifo_read
open success
read from FIFO
这里没有读到信息,问题有待继续查。。。