PIPE和socketpair的区别
2016-02-20 15:54
411 查看
2013年04月19日 ⁄ 综合 ⁄ 共 1580字 ⁄ 字号 小 中 大 ⁄ 评论关闭
<iframe id="iframeu1788635_0" src="http://pos.baidu.com/acom?rdid=1788635&dc=2&di=u1788635&dri=0&dis=0&dai=2&ps=236x909&dcb=BAIDU_UNION_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1455954703440&ti=%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%EF%BC%9A%E7%AE%A1%E9%81%93%EF%BC%88pipe%EF%BC%89%E5%92%8Csocketpair%E5%8C%BA%E5%88%AB%20%7C%20%E5%AD%A6%E6%AD%A5%E5%9B%AD&ari=1&dbv=2&drs=1&pcs=1366x643&pss=1366x256&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1455954703<u=http%3A%2F%2Fwww.xuebuyuan.com%2F1722397.html<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DizVtXr3siFLYNGQmL5NP7RshCiyR73V6ICTsiSNln3hADtaI6XSN1LSWZeWHOE-X%26wd%3D%26eqid%3D81733d71000aa30b0000000356c819fc&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=true&cmi=7&col=zh-CN&cdo=-1&tcn=1455954704&qn=bd9b98088efa9e6a&tt=1455954703410.95.342.346" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; font-size: 13px; vertical-align: bottom; background: transparent;"></iframe>
管道pipe是半双工的,pipe两次才能实现全双工,使得代码复杂。socketpair直接就可以实现全双工
socketpair对两个文件描述符中的任何一个都可读和可写,而pipe是一个读,一个写
详间代码:
一:pipe实现父子进程全双工通信:
输出结果:parent Sending data: 1
son Data received: 1
son send received: 2
parent Data received: 2
parent Sending data: 3
son Data received: 3
son send received: 4
parent Data received: 4
一:soketpair实现父子进程全双工通信:
输出结果:parent Sending data: 1
son Data received: 1
son send received: 2
parent Data received: 2
parent Sending data: 3
son Data received: 3
son send received: 4
parent Data received: 4
<iframe id="iframeu1788635_0" src="http://pos.baidu.com/acom?rdid=1788635&dc=2&di=u1788635&dri=0&dis=0&dai=2&ps=236x909&dcb=BAIDU_UNION_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1455954703440&ti=%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%EF%BC%9A%E7%AE%A1%E9%81%93%EF%BC%88pipe%EF%BC%89%E5%92%8Csocketpair%E5%8C%BA%E5%88%AB%20%7C%20%E5%AD%A6%E6%AD%A5%E5%9B%AD&ari=1&dbv=2&drs=1&pcs=1366x643&pss=1366x256&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1455954703<u=http%3A%2F%2Fwww.xuebuyuan.com%2F1722397.html<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DizVtXr3siFLYNGQmL5NP7RshCiyR73V6ICTsiSNln3hADtaI6XSN1LSWZeWHOE-X%26wd%3D%26eqid%3D81733d71000aa30b0000000356c819fc&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=true&cmi=7&col=zh-CN&cdo=-1&tcn=1455954704&qn=bd9b98088efa9e6a&tt=1455954703410.95.342.346" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; font-size: 13px; vertical-align: bottom; background: transparent;"></iframe>
管道pipe是半双工的,pipe两次才能实现全双工,使得代码复杂。socketpair直接就可以实现全双工
socketpair对两个文件描述符中的任何一个都可读和可写,而pipe是一个读,一个写
详间代码:
一:pipe实现父子进程全双工通信:
#include <stdlib.h> #include <stdio.h> int main () { int fd1[2],fd2[2]; pipe(fd1); pipe(fd2); if ( fork() ) { /* Parent process: echo client */ int val = 0; close( fd1[0] ); close(fd2[1]); while ( 1 ) { sleep( 1 ); ++val; printf( "parent Sending data: %d\n", val ); write( fd1[1], &val, sizeof(val) ); read( fd2[0], &val, sizeof(val) ); printf( "parent Data received: %d\n", val ); } } else { /* Child process: echo server */ int val ; close( fd1[1] ); close(fd2[0]); while ( 1 ) { read( fd1[0], &val, sizeof(val) ); printf( "son Data received: %d\n", val ); ++val; write( fd2[1], &val, sizeof(val) ); printf( "son send received: %d\n", val ); } } }
输出结果:parent Sending data: 1
son Data received: 1
son send received: 2
parent Data received: 2
parent Sending data: 3
son Data received: 3
son send received: 4
parent Data received: 4
一:soketpair实现父子进程全双工通信:
#include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <stdio.h> int main () { int fd[2]; int r = socketpair( AF_UNIX, SOCK_STREAM, 0, fd ); if ( r < 0 ) { perror( "socketpair()" ); exit( 1 ); } if ( fork() ) { /* Parent process: echo client */ int val = 0; close( fd[1] ); while ( 1 ) { sleep( 1 ); ++val; printf( "parent Sending data: %d\n", val ); write( fd[0], &val, sizeof(val) ); read( fd[0], &val, sizeof(val) ); printf( "parent Data received: %d\n", val ); } } else { /* Child process: echo server */ int val ; close( fd[0] ); while ( 1 ) { read( fd[1], &val, sizeof(val) ); printf( "son Data received: %d\n", val ); ++val; write( fd[1], &val, sizeof(val) ); printf( "son send received: %d\n", val ); } } }
输出结果:parent Sending data: 1
son Data received: 1
son send received: 2
parent Data received: 2
parent Sending data: 3
son Data received: 3
son send received: 4
parent Data received: 4
相关文章推荐
- 四、再谈PyTypeObject http://blog.csdn.net/debugm/article/details/8243496
- 自己生成非官方iPhone toolchain的头文件
- HDU 2476 String painter(区间dp)
- pcie干货系列之-TLP’s size/TLP fail/4K boundary/requs
- Codeforces 630A Again Twenty Five!
- saiku、mondrian前奏之——立方体、维度、Schema的基本概念
- saiku、mondrian前奏之——立方体、维度、Schema的基本概念
- [Locked] Paint House I & II
- 无线高清会议室终端之Airplay
- 分享自yebai445791253 《Verilog $random用法》
- trait中的属性类型
- SVM实用操作: svmtrain and svmclassify
- 【Mybatis】Parameter 'xxx' not found. Available parameters are [1, 0, param1, param2]
- CodeForces-630A. Again Twenty Five!【找规律】
- bzoj 3172 [Tjoi2013]单词(fail树,DP)
- 人工智能和机器学习领域的一些有趣的开源项目
- HDU 3487 Play with Chain
- 趣说游戏AI开发:曼哈顿街角的A*算法
- AIDL简单使用(通过它跨应用传递对象(数据))
- arch/arm/lib/crt0.S main主函数