分类: LINUX
2010-11-19 15:40:51
mknod创建FIFO
无名管道的最大劣势就是只能用于一个共同祖先进程的各个进程之间,无亲缘关系的两个进程无法创建一个彼此间的管道并将它用作IPC通道。FIFO(first inf,first out)是unix中的有名管道,FIFO类似于管道,传输单向数据流。
以下是FIFO读写的例子:
Server.c
#include #include #include #include #include #include #define FIFO_FILE "tempFIFO" int main(int argc,char **argv) { FILE *fp; char readbuff[80]; umask(0); mknod(FIFO_FILE,S_IFIFO|0666,0); while(1) { if((fp = fopen(FIFO_FILE,"r")) == NULL) { perror("fopen"); exit(1); } fgets(readbuff,80,fp); printf("receive data from fifo is:%s.\n",readbuff); fclose(fp); } exit(0); } |
Client.c
#include #include #include #include #include #include #define FIFO_FILE "tempFIFO" int main(int argc,char **argv) { FILE *fp; char readbuff[80]; if((fp = fopen(FIFO_FILE,"w")) == NULL) { perror("fopen"); exit(1); } fputs("hello,world!",fp); fclose(fp); exit(0); } |
启动server
mark@ubuntu:~/fifo_mknod$ ./server |
启动client
mark@ubuntu:~/fifo_mknod$ ls client client.c Makefile server server.c tempFIFO mark@ubuntu:~/fifo_mknod$ ./client |
运行client后在server处终端显示server处从FIFO读取到的信息:
mark@ubuntu:~/fifo_mknod$ ./server receive data from fifo is:hello,world!. |
chinaunix网友2010-11-20 16:37:07
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com