当前位置:网站首页>进程间通信方式(1)无名管道(全CSDN最用心的博主)
进程间通信方式(1)无名管道(全CSDN最用心的博主)
2022-08-11 01:22:00 【畅畅ccc】
在前几篇文章当中我们为大家详细的讲述了进程和线程基本和进阶应用,那么不同的进程和线程当中如果我们进行数据的交互,或者信息的访问,我们应该采取什么办法呢?
那么本文将为大家讲述进程间的通信方式-----管道中的无名管道
管道分为无名管道和有名管道
- 无名管道 ===》pipe ==》只能给有亲缘关系进程通信
- 有名管道 ===》fifo ==》可以给任意单机进程通信
管道的特性:
- 管道是 半双工的工作模式
- 所有的管道都是特殊的文件不支持定位操作。(lseek->> fd fseek ->>FILE* )
- 管道是特殊文件,读写使用文件IO。(open,read,write,close)
使用框架:
- 创建管道 == 》 读写管道 ==》关闭管道
1.创建管道
int pipe(int pipefd[2]);
- 功能:创建并打开一个无名管道
- 参数:
- pipefd[0] ==>无名管道的固定读端
- pipefd[1] ==>无名管道的固定写端
- 返回值:
- 成功 0
- 失败 -1
2.读写管道
把pipefd当作文件描述符进行操作,pipefd[0] ==>无名管道的固定读端,pipefd[1] ==>无名管道的固定写端,pipefd的位置是固定的,在使用时要记得
3.关闭管道
close pipefd[0];
close pipefd[1];
关闭管道也没有特殊的函数接口,和关闭文件描述符一样,在文件IO的操作里运用close的指令操作。
代码示例:
int main(int argc, const char *argv[]) { int pipefd[2]; int ret = pipe(pipefd); if(ret < 0) { perror("fail to pipe\n"); return -1; } pid_t pid = fork(); if(pid > 0) { close(pipefd[0]); while(1) { char buff[1024] = {0}; fgets(buff, sizeof(buff), stdin); write(pipefd[1], buff, strlen(buff)+1); if(0 == strcmp(buff, "quit\n")) { break; } } wait(NULL); close(pipefd[1]); } else if(0 == pid) { close(pipefd[1]); while(1) { char buff[1024] = {0}; read(pipefd[0], buff, sizeof(buff)); if(0 == strcmp(buff, "quit\n")) { break; } printf("your write:%s", buff); } close(pipefd[0]); } else { perror("fail to fork\n"); return -1; } return 0; }以上代码就是在父子进程中使用了pipe管道进行父子进程间的通信,父进程终端输入内容,在子进程中可以打印出来并且quit退出,以上要注意pipefd[0]和pipefd[1]端口的读写顺序,pipefd类似于文件描述符,要用文件IOwrite和read进行读写,用后记得关闭管道pipefd,避免浪费资源
边栏推荐
- apache+PHP+MySQL+word press, page error when installing word press?
- php 判断数组是否为多维数组
- 分库分表ShardingSphere-JDBC笔记整理
- 池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!
- 什么是数组
- Kunpeng compilation and debugging and basic knowledge of native development tools
- Data Analysis Interview Manual "SQL"
- 微信小程序内部A页面向内嵌H5页面跳转,并且传参
- postgresql参数意义
- Shell 文本三剑客 Sed
猜你喜欢

C # - delegate detailed usage

两日总结九

阿里的数据同步神器——Canal

微信小程序通过URL Scheme动态的渲染数据

异常:try catch finally throws throw
![[GXYCTF2019]BabySQli](/img/8a/7500c0ee275d6ef8909553f34c99cf.png)
[GXYCTF2019]BabySQli

Construction inspection, no rules and no square

apache+PHP+MySQL+word press,安装word press时页面报错?

Apache Commons Configuration Remote Code Execution Vulnerability (CVE-2022-33980) Analysis & Reproduction

颠覆性创新招商,链动2+1是个怎么样的制度模式?
随机推荐
二维数组实战项目--------《扫雷游戏》
构建资源的弹性伸缩
16. Sum of the nearest three numbers
两日总结十
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
划分字母区间[贪心->空间换时间->数组hash优化]
编程技巧│selenium 更新 chromedriver 驱动
什么是数组
微信小程序获取当前页面的url和参数
Apache Commons Configuration远程代码执行漏洞(CVE-2022-33980)分析&复现
R语言多元线性回归、ARIMA分析美国不同候选人对经济GDP时间序列影响
③ 关系数据库标准语言SQL 数据查询(SELECT)
WinForm (5) control and its members
what is an array
paddle2.3和torch1.8在SentenceBert上的性能对比
Linux install redis database
Shell Text Three Musketeers Sed
apache+PHP+MySQL+word press, page error when installing word press?
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
双机热备综合实验(VRRP+OSPF+VTP+NAT+DHCP+PVSTP+单臂路由)