管道例子

无名管道例子
  1. #include <stdio.h>   
  2. #include <unistd.h>   
  3. #include <string.h>   
  4. #define MAX_LINE 80   
  5. #define PIPE_STDIN 0   
  6. #define PIPE_STDOUT 1   
  7. /*  
  8.   myPipe[ 1 ]向管道写入数据;myPipe[ 0 ]从管道读取数据。  
  9.  */  
  10. int main(  )   
  11.     {   
  12.         const char* string={"A simple message."};   
  13.         int ret,myPipe[ 2 ];   
  14.         char buffer[ MAX_LINE+1 ];   
  15.         //create the pipe   
  16.         ret=pipe( myPipe );   //pipe(  )创建一个匿名管道   
  17.         if( ret==0 )   
  18.             {   
  19.                 //write the message into the pipe   
  20.                 write( myPipe[ PIPE_STDOUT ],string,strlen( string ) );   
  21.                 //read the message from the pipe   
  22.                 ret=read( myPipe[ PIPE_STDIN ],buffer,MAX_LINE );   
  23.                 //NULL terminate the string   
  24.                 buffer[ ret ]=0;   
  25.                 printf( "%s\n",buffer );   
  26.                    
  27.             }   
  28.         close( thePipe[ 0 ] );   
  29.         close( thePipe[ 1 ] );   
  30.            
  31.         return 0;   
  32.     }  
  33. 结果:A simple message.

  1. //父子进程间利用管道通讯实例   
  2. #include <stdio.h>   
  3. #include <unistd.h>   
  4. #include <string.h>   
  5. #include <wait.h>   
  6. #define MAX_LINE 80   
  7. int main(  )   
  8.     {   
  9.         int thePipe[ 2 ],ret;   
  10.         char buf[ MAX_LINE+1 ];   
  11.         const char* testbuf={"a test string."};   
  12.         if( pipe( thePipe )==0 )   
  13.             {   
  14.                 if( fork(  )==0 )   
  15.                     {   
  16.                         printf( "You have enter the child process\n" );   
  17.                         ret=read( thePipe[ 0 ],buf,MAX_LINE );   
  18.                         buf[ ret ]=0;   
  19.                         printf( "Child read info: %s\n",buf );   
  20.                            
  21.                     }   
  22.                 else  
  23.                     {   
  24.                         ret=write( thePipe[ 1 ],testbuf,strlen( testbuf ) );   
  25.                         ret=wait( NULL );   
  26.                            
  27.                     }   
  28.             }   
  29.         close( thePipe[ 0 ] );   
  30.         close( thePipe[ 1 ] );   
  31.            
  32.         return 0;   
  33.            
  34.     }   
  35. /*值得注意的是:  
  36.   把子进程的输出重定向到管道的输入,父进程的输入重定向到管道的输出。  
  37.   --这是一个很值得记住的有用技术  
  38.  */  
  1. //使用C实现管道链接   
  2. #include <stdio.h>   
  3. #include <stdlib.h>   
  4. #include <unistd.h>   
  5. int main(  )   
  6.     {   
  7.         int pfds[ 2 ];   
  8.         if( pipe( pfds )==0 )   
  9.             {   
  10.                 if( fork(  )==0 )   
  11.                     {   
  12.                         close( 1 );   
  13.                         dup2( pfds[ 1 ],1 );   
  14.                         close( pfds[ 0 ] );   
  15.                         execlp( "ls","ls","-l",NULL );   
  16.                            
  17.                     }   
  18.                 else  
  19.                     {   
  20.                         close( 0 );   
  21.                         dup2( pfds[ 0 ],0 );   
  22.                         close( pfds[ 1 ] );   
  23.                         execlp( "wc","wc","-l",NULL );   
  24.                            
  25.                     }   
  26.             }   
  27.         return 0;   
  28.            
  29.     }  
pipe(建立管道)
表头文件 #include
定义函数 int pipe(int filedes[2]);
函数说明
    pipe()会建立管道,并将文件描述词由参数 filedes 数组返回。
    filedes[0]为管道里的读取端,所以pipe用read调用的
    filedes[1]则为管道的写入端。
    
返回值:  若成功则返回零,否则返回-1,错误原因存于 errno 中。
错误代码: 
    EMFILE 进程已用完文件描述词最大量
    ENFILE 系统已无文件描述词可用。
    EFAULT 参数 filedes 数组地址不合法。
#include 
#include 
int main( void )
{
    int filedes[2];
    char buf[80];
    pid_t pid;
    
    pipe( filedes );
    
    if ( (pid=fork()) > 0 )
    {
        printf( "This is in the father process,here write a string to the pipe.\n" );
        char s[] = "Hello world , this is write by pipe.\n";
        write( filedes[1], s, sizeof(s) );
        close( filedes[0] );
        close( filedes[1] );
    }
    else
    {
        printf( "This is in the child process,here read a string from the pipe.\n" );
        read( filedes[0], buf, sizeof(buf) );
        printf( "%s\n", buf );
        close( filedes[0] );
        close( filedes[1] );
    }
    
    waitpid( pid, NULL, 0 );
    
    return 0;
}
[root@localhost src]# gcc pipe.c 
[root@localhost src]# ./a.out 
This is in the child process,here read a string from the pipe.
This is in the father process,here write a string to the pipe.
Hello world , this is write by pipe.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值