linux 简单的命名管道
来源:互联网 发布:软件发短信 编辑:程序博客网 时间:2024/05/05 04:10
命名管道,使用双向通信,要么分时复用,要么创建2个管道一个专业收,一个接。
1.使用mkfifo函数创管道;
2.使用open函数打开管道;
3.使用read,write读写管道,不要两个程序同时对一个管道进行写,这样会阻塞的;
4.使用close关闭打开的文件描述符。
下面使用一个简单的例程实现两个程序之间的通信,大致流程如下:第一个文件为w.c:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(){int fd_w = -1;int fd_r = -1;int i = 0;unsigned char val[128];memset(val,0,128);unlink("/tmp/read");unlink("/tmp/write");mkfifo("/tmp/read",0777);mkfifo("/tmp/write",0777);fd_r = open("/tmp/read",O_RDWR);fd_w = open("/tmp/write",O_RDWR);printf("start read and write\n");for (i = 0; i < 100; i++){write(fd_w,"hwllo world",12);read(fd_r,val,12);printf("receive data : %s\n",val);sleep(1);}close(fd_w);close(fd_r);unlink("/tmp/read");unlink("/tmp/write");return 0;}
第二个程序 r.c:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(){int fd_w = -1;int fd_r = -1;int i = 0;unsigned char val[128];memset(val,0,128);fd_w = open("/tmp/read",O_RDWR);fd_r = open("/tmp/write",O_RDWR);printf("start read and write\n");for (i = 0; i < 100; i++){write(fd_w,"hiboy world",12);read(fd_r,val,12);printf("receive data : %s\n",val);sleep(1);}close(fd_w);close(fd_r);unlink("/tmp/read");unlink("/tmp/write");return 0;}
编译上面两个程序:
#gcc w.c -o w#gcc r.c -o r执行上面程序,首先执行w,然后执行r。因为管道是在w程序中创建的。
#./w &#./r
看控制台是否有信息输出。
0 0
- linux 简单的命名管道
- 命名管道的简单使用
- 命名管道的简单使用
- Linux下利用命名管道实现简单的聊天室
- linux的管道和命名管道
- linux的管道和命名管道
- linux的管道和命名管道
- linux c之命名管道简单使用
- 命名管道的一个简单例子
- 命名管道--简单的客户服务器通信
- Linux命名管道FIFO的读写规则
- Linux命名管道FIFO的读写规则
- Linux命名管道FIFO的读写规则
- Linux命名管道FIFO的读写规则
- linux 匿名、命名管道的实现
- linux 命名管道
- Linux命名管道FIFO
- Linux命名管道
- eclipse打开文件或者目录位置
- mm_server
- C语言设计---scheme表示
- Quartz2D 图片水印
- CentOS 6.2 64位 手动安装Apache 2.4.9
- linux 简单的命名管道
- htmlview屏蔽滚动条的问题
- 黑马程序员___集合2
- 六月过一旬了,下面剩下不到20天写个简短的计划吧
- CMake简单使用参考
- Python中的join和split
- 编译原理中FIRST集合与FOLLOW集合的算法
- JAVA中protected的作用
- 栈的应用--计算字符串表达式