mknod创建FIFO
时间:2010-11-19 来源:unix_disciple
mknod创建FIFO
无名管道的最大劣势就是只能用于一个共同祖先进程的各个进程之间,无亲缘关系的两个进程无法创建一个彼此间的管道并将它用作IPC通道。FIFO(first inf,first out)是unix中的有名管道,FIFO类似于管道,传输单向数据流。
以下是FIFO读写的例子:
Server.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h>
#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 <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h>
#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!. |