【C语言】【unix c】如何创建有名管道
来源:互联网 发布:淘宝流量充值怎么开店 编辑:程序博客网 时间:2024/06/06 02:27
有名管道其实是一个文件,这个文件只能用于两个进程间通信的桥梁,不存储任何数据内容 如何创建有名管道 : mkfifo(3) #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode); 功能:创建一个有名管道的文件 参数: pathname:指定了有名管道的名字 mode:指定了管道文件的权限 返回值:成功 0 错误 -1 举例:有名管道文件的创建(mkfifo.c)文件名由命令行传入,权限0664 创建管道文件: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> int main(int argc, char *argv[]) { int m = mkfifo(argv[1],0664); if(m == -1) { perror("mkfifo"); return -1; } return 0; } 测试: 编写代码向有名管道写数据(PA.c) #include <stdio.h> #include <p_file.h> #include <string.h> int main(int argc, char *argv[]) { char *msg = "this is a test!\n"; int fd =open(argv[1], O_WRONLY); if(fd == -1) { perror("open"); return -1; } write(fd, msg, strlen(msg)); close(fd); return 0; } 编写代码从有名管道读取数据(PB.c) #include <stdio.h> #include <p_file.h> #include <string.h> int main(int argc, char *argv[]) { char buf[128]; int fd =open(argv[1], O_RDONLY); if(fd == -1) { perror("open"); return -1; } int r = read(fd, buf, 128); write(1, buf, r); close(fd); return 0; } 运行: PA: tarena@ubuntu:~/day/day32$ A hello - PB: tarena@ubuntu:~/day/day32$ B hello 先运行PA后运行PB的结果:this is a test!
阅读全文
0 0
- 【C语言】【unix c】如何创建有名管道
- 【C语言】【unix c】如何创建无名管道
- C语言有名管道fifo的创建及运用
- UNIX下C语言----管道
- UNIX下C语言----管道
- 【C语言】【unix c】如何使用管道实现两个进程间的通信
- Linux C 父子进程 与 有名管道
- 【C语言】【unix c】进程的创建
- 【C语言】【unix c】进程的创建
- 【C语言】【unix c】线程的创建
- 有名管道的创建
- 创建有名管道
- 创建有名管道
- Linux c 管道和有名管道(FIFO)
- Linux下的C语言编程——共享内存及有名管道的使用
- [Linux C编程]有名管道实现守护进程
- Linux c==进程通信--有名管道(4)
- mkfifo函数创建有名管道
- Python入门新人,求助
- 使用Visual Studio快速开发STM32F4-Discovery探索板入门
- kafka学习
- Kotlin学习笔记二(数据类型)
- 求大神反编译个程序
- 【C语言】【unix c】如何创建有名管道
- 等比矩阵求和-POJ3233
- 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
- Fast Fourier Transform
- javascript之BOM对象(三其他对象)
- Diary I
- android 百度搜索接口
- 静态代理,动态代理,Cglib代理
- NLP深度学习 —— CS224学习笔记9