您的位置:首页 > 大数据 > 人工智能

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&ltu=http%3A%2F%2Fwww.xuebuyuan.com%2F1722397.html&ltr=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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: