Linux管道编程实例 http://blog.csdn.net/pcliuguangtao/article/details/6453743

 

Linux管道编程实例

分类: Unix/Linux/BSD   2542人阅读  评论(0)  收藏  举报

[cpp]  view plain copy
  1. /*管道 
  2.   可以把管道想象为两个实体之间的单向连接器。注意,管道是半双工的, 
  3.   如果需要全双工通讯,应该转而考虑套接字。 
  4.   匿名管道又称管道,提供了一个进程与它的兄弟进程通讯的方法,只存在于父进程中; 
  5.   命名管道,可以存在与文件系统中,任意进程都可找到它,使得不同先祖的进程也可以通讯。 
  6.   #include <unistd.h> 
  7.   int pipe( int dfs[ 2 ] );创建匿名管道 
  8.   int dup(int oldfd );创建一个文件描述符的副本 
  9.   int dup2(int oldfd, int targetfd); 
  10.   dup/dup2提供了复制文件描述符的功能。他们常用于stdin(0)、stdout(1)、stderr(2)的重定向; 
  11.   #include <sys/types.h> 
  12.   #include <sys/stat.h> 
  13.   int mkfifo(const char* pathname,mode_t mode  );创建一个命名管道 
  14.   记住:管道只不过是一对文件描述符因此所有能够操作文件描述符的函数都可用于管道。这些函数 
  15.   包括但不限于select,read,write,fcntl,freopen。 
  16.  */  
  17. /**********1、简单匿名管道应用************/  
  18. #include <stdio.h>  
  19. #include <unistd.h>  
  20. #include <string.h>  
  21. #define MAX_LINE 80  
  22. #define PIPE_STDIN 0  
  23. #define PIPE_STDOUT 1  
  24. /* 
  25.   myPipe[ 1 ]向管道写入数据;myPipe[ 0 ]从管道读取数据。 
  26.  */  
  27. int main(  )  
  28.     {  
  29.         const char* string={"A simple message."};  
  30.         int ret,myPipe[ 2 ];  
  31.         char buffer[ MAX_LINE+1 ];  
  32.         //create the pipe  
  33.         ret=pipe( myPipe );   //pipe(  )创建一个匿名管道  
  34.         if( ret==0 )  
  35.             {  
  36.                 //write the message into the pipe  
  37.                 write( myPipe[ PIPE_STDOUT ],string,strlen( string ) );  
  38.                 //read the message from the pipe  
  39.                 ret=read( myPipe[ PIPE_STDIN ],buffer,MAX_LINE );  
  40.                 //NULL terminate the string  
  41.                 buffer[ ret ]=0;  
  42.                 printf( "%s/n",buffer );  
  43.                   
  44.             }  
  45.         close( thePipe[ 0 ] );  
  46.         close( thePipe[ 1 ] );  
  47.           
  48.         return 0;  
  49.     }  
  50.   
  51. //父子进程间利用管道通讯实例  
  52. #include <stdio.h>  
  53. #include <unistd.h>  
  54. #include <string.h>  
  55. #include <wait.h>  
  56. #define MAX_LINE 80  
  57. int main(  )  
  58.     {  
  59.         int thePipe[ 2 ],ret;  
  60.         char buf[ MAX_LINE+1 ];  
  61.         const char* testbuf={"a test string."};  
  62.         if( pipe( thePipe )==0 )  
  63.             {  
  64.                 if( fork(  )==0 )  
  65.                     {  
  66.                         printf( "You have enter the child process/n" );  
  67.                         ret=read( thePipe[ 0 ],buf,MAX_LINE );  
  68.                         buf[ ret ]=0;  
  69.                         printf( "Child read info: %s/n",buf );  
  70.                           
  71.                     }  
  72.                 else  
  73.                     {  
  74.                         ret=write( thePipe[ 1 ],testbuf,strlen( testbuf ) );  
  75.                         ret=wait( NULL );  
  76.                           
  77.                     }  
  78.             }  
  79.         close( thePipe[ 0 ] );  
  80.         close( thePipe[ 1 ] );  
  81.           
  82.         return 0;  
  83.           
  84.     }  
  85. /*值得注意的是: 
  86.   把子进程的输出重定向到管道的输入,父进程的输入重定向到管道的输出。 
  87.   --这是一个很值得记住的有用技术 
  88.  */  
  89. //使用C实现管道链接  
  90. #include <stdio.h>  
  91. #include <stdlib.h>  
  92. #include <unistd.h>  
  93. int main(  )  
  94.     {  
  95.         int pfds[ 2 ];  
  96.         if( pipe( pfds )==0 )  
  97.             {  
  98.                 if( fork(  )==0 )  
  99.                     {  
  100.                         close( 1 );  
  101.                         dup2( pfds[ 1 ],1 );  
  102.                         close( pfds[ 0 ] );  
  103.                         execlp( "ls","ls","-l",NULL );  
  104.                           
  105.                     }  
  106.                 else  
  107.                     {  
  108.                         close( 0 );  
  109.                         dup2( pfds[ 0 ],0 );  
  110.                         close( pfds[ 1 ] );  
  111.                         execlp( "wc","wc","-l",NULL );  
  112.                           
  113.                     }  
  114.             }  
  115.         return 0;  
  116.           
  117.     }  

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值