【linux】——进程间的通信之管道通信
2013-06-10 15:33
441 查看
通过创建命名管道实现任何一个进程的通信:
mkfifo_read.c
mkfifo_write.c
注:
一:O_NONBLOCK 如果pathname指的是一个FIFO、一个特殊文件或一个字符特殊文件,则此选项为文件的本次打开操作和后续的I/O操作设置非阻塞模式。
mkfifo_read.c
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> #define FIFO "text" int main(int argc, char *argv[]) { int fd; int mkfi; int fd_read; char buf[100]; if(mkfifo(FIFO,O_CREAT|O_EXCL) < 0) { printf("mkfifo error!\n"); exit(1); } if((fd = open(FIFO,O_RDONLY|O_NONBLOCK,0)) == -1) { printf("open fifo error!\n"); exit(1); } while(1) { memset(buf,0,sizeof(buf)); if(fd_read = read(fd,buf,100) == -1) printf("this is no data!\n"); else printf("%s\n",buf); sleep(1); } }
mkfifo_write.c
#include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #define FIFO "text" int main(int argc, char *argv[]) { int fd; int nwrite; if((fd = open(FIFO,O_WRONLY|O_NONBLOCK,0)) == -1) { printf("open FIFO error!\n"); exit(1); } if((nwrite = write(fd,argv[1],100)) == -1) { printf("error!\n"); } else printf("write %s\n",argv[1]); }
注:
一:O_NONBLOCK 如果pathname指的是一个FIFO、一个特殊文件或一个字符特殊文件,则此选项为文件的本次打开操作和后续的I/O操作设置非阻塞模式。
相关文章推荐
- linux基础编程:进程通信之管道
- Linux下的进程通信方式: 管道通信详解
- Linux下的有名管道(05)---使用两个管道实现两个进程之间的通信(对讲机模式)
- Linux进程通信总结(二) --管道
- Linux基础篇 进程通信——管道
- Linux操作系统分析(10) - 进程通信之管道与信号量
- Linux下进程通信之管道
- Linux进程通信:管道要点
- LINUX编程——进程间管道通信
- Linux----进程间通信-管道与两个命名管道实现进程双向通信
- linux进程通信--管道
- Linux进程间的通信——管道
- linux 进程通信 ----管道
- Linux操作系统分析(10) - 进程通信之管道与信号量
- linux进程间管道通信pipe与fifo
- Linux进程通信:命名管道FIFO小结
- 服务器serverbuffergcc数据结构Linux进程通信:命名管道FIFO小结
- linux进程通信之使用匿名管道进行父子进程通信
- linux中利用有名管道实现进程之间的通信
- linux的进程通信-管道