linux系统编程:进程间通信-fifo
来源:互联网 发布:红警网络进不去 编辑:程序博客网 时间:2024/03/29 13:30
进程间通信-fifo
进程间通信的另一种方式是fifo。fifo是另一种管道:有名管道。从名字可以看出,它也是队列。
使用fifo通信前,得先创建fifo
$ mkfifo myfifo
fifo_w.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/fcntl.h>struct stu{int id;char name[20];};int main(int argc, char **argv){if(argc != 2){fprintf(stderr, "usage:./app fifo\n");exit(1);}int fd;if((fd = open(argv[1], O_WRONLY)) < 0){fprintf(stderr, "open:can not open file:%s\n", argv[1]);exit(1);}struct stu zx = {0, "zhangxiang"};int id = 0;while(1){id++;zx.id = id;write(fd, &zx, sizeof(zx));sleep(1);}close(fd);return 0;}
fifo_r.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/fcntl.h>struct stu{int id;char name[20];};int main(int argc, char **argv){if(argc != 2){fprintf(stderr, "usage:./app fifo");exit(1);}int fd;if((fd = open(argv[1], O_RDONLY)) < 0){fprintf(stderr, "open:can not open file:%s", argv[1]);exit(1);}struct stu zx;while(1){read(fd, &zx, sizeof(zx));printf("id=%d,name=%s\n", zx.id, zx.name);sleep(1);}close(fd);return 0;}
$ gcc fifo_w.c -o fifo_w$ gcc fifo_r.c -o fifo_r$ fifo_w myfifo//另开一终端$ fifo_r myfifoid=1,name=zhangxiangid=2,name=zhangxiangid=3,name=zhangxiangid=4,name=zhangxiangid=5,name=zhangxiangid=6,name=zhangxiangid=7,name=zhangxiangid=8,name=zhangxiang^c
以上示例中,一个进程不断地向fifo中写入结构体类型的数据,另一个进程不断地从fifo中读出数据,从而达到进程间的通信。
CCPP Blog 目录
1 0
- linux系统编程:进程间通信-fifo
- linux 系统编程-学习笔记10--进程间通信--管道/FIFO/消息队列/
- Linux系统编程——进程间通信:命名管道(FIFO)
- Linux系统编程——进程间通信:命名管道(FIFO)
- Linux系统编程——进程间通信:命名管道(FIFO)
- linux 进程间通信 FIFO
- Linux编程练习 --进程间通信3--FIFO命名管道
- Linux环境编程之IPC进程间通信(三):FIFO
- linux编程---进程间通信---FIFO---有名管道
- Linux进程通信 FIFO
- Linux进程间通信: FIFO(1)
- Linux进程间通信: FIFO(2)
- linux C-FIFO(进程间通信)
- Linux进程间通信-FIFO管道
- 【Linux】进程间通信-命名管道FIFO
- Linux进程间通信方式----FIFO
- linux进程间通信之FIFO
- 【Linux】进程间通信-命名管道FIFO
- 使用 Feed4JUnit 进行数据与代码分离的 Java 单元测试
- Oracle数据库备份与恢复 - RMAN恢复
- Roll-a-Ball教程学习笔记(未整理)
- Codeforces Round #312 (Div. 2)
- TileSream服务器搭建
- linux系统编程:进程间通信-fifo
- 往MySQL中存储图片
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
- YT06-背包-1003—悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 -(6.27日-烟台大学ACM预备队解题报告)
- Java中的重载、重写、多态,静态绑定、动态绑定
- [Elasticsearch] 控制相关度 (一) - 相关度分值计算背后的理论
- MATLAB中常用的产生随机数的几种函数
- unity3D的FingerGestures插件
- Redhat EL服务器命令行下U盘的使用