PIPE和socketpair的区别

来源:互联网 发布:通过ip查mac地址命令 编辑:程序博客网 时间:2024/04/24 15:53
2013年04月19日 ⁄ 综合 ⁄ 共 1580字 ⁄ 字号 小 中 大 ⁄ 评论关闭
<iframe id="iframeu1788635_0" src="http://pos.baidu.com/acom?rdid=1788635&amp;dc=2&amp;di=u1788635&amp;dri=0&amp;dis=0&amp;dai=2&amp;ps=236x909&amp;dcb=BAIDU_UNION_define&amp;dtm=BAIDU_DUP_SETJSONADSLOT&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1455954703440&amp;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&amp;ari=1&amp;dbv=2&amp;drs=1&amp;pcs=1366x643&amp;pss=1366x256&amp;cfv=0&amp;cpl=5&amp;chi=1&amp;cce=true&amp;cec=UTF-8&amp;tlm=1455954703&amp;ltu=http%3A%2F%2Fwww.xuebuyuan.com%2F1722397.html&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DizVtXr3siFLYNGQmL5NP7RshCiyR73V6ICTsiSNln3hADtaI6XSN1LSWZeWHOE-X%26wd%3D%26eqid%3D81733d71000aa30b0000000356c819fc&amp;ecd=1&amp;psr=1366x768&amp;par=1366x728&amp;pis=-1x-1&amp;ccd=24&amp;cja=true&amp;cmi=7&amp;col=zh-CN&amp;cdo=-1&amp;tcn=1455954704&amp;qn=bd9b98088efa9e6a&amp;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

0 0
原创粉丝点击