Chinaunix首页 | 论坛 | 博客
  • 博客访问: 149284
  • 博文数量: 69
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 602
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-25 20:56
文章分类

全部博文(69)

文章存档

2015年(68)

2014年(1)

我的朋友

分类: LINUX

2015-01-11 22:20:42

  1. /*管道  
  2.   可以把管道想象为两个实体之间的单向连接器。注意,管道是半双工的,  
  3.   如果需要全双工通讯,应该转而考虑套接字。  
  4.   匿名管道又称管道,提供了一个进程与它的兄弟进程通讯的方法,只存在于父进程中;  
  5.   命名管道,可以存在与文件系统中,任意进程都可找到它,使得不同先祖的进程也可以通讯。  
  6.   #include   
  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   
  12.   #include   
  13.   int mkfifo(const char* pathname,mode_t mode  );创建一个命名管道  
  14.   记住:管道只不过是一对文件描述符因此所有能够操作文件描述符的函数都可用于管道。这些函数  
  15.   包括但不限于select,read,write,fcntl,freopen。  
  16.  */  
  17. /**********1、简单匿名管道应用************/  
  18. #include    
  19. #include    
  20. #include    
  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    
  53. #include    
  54. #include    
  55. #include    
  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    
  91. #include    
  92. #include    
  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.     }  
阅读(751) | 评论(0) | 转发(0) |
0

上一篇:进程组

下一篇:linux管道定义

给主人留下些什么吧!~~