linux c 进程间通信实例代码总结

http://www.tldp.org/LDP/lpg/node7.html

linux管道之 pipe()函数的使用

 

pipe(建立管道):
表头文件: #include
定义函数: int pipe(int filedes[2]);
函数说明: pipe()会建立管道,并将文件描述词由参数filedes数组返回。
           filedes[0]为管道里的读取端filedes[1]则为管道的写入端
返回值:  若成功则返回零,否则返回-1,错误原因存于errno中。

实例:

 


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;
}

另外,说明一下管道读写的阻塞。

当管道中的数据被读取后,管道为空。一个随后的read()调用将默认的被阻塞,等待某些数据写入。


Linux管道pipe使用实例

标签: linuxbuffersystem存储
  3188人阅读  评论(0)  收藏  举报
  分类:
 

函数

#include <unistd.h>

int pipe(int filedes[2]);

描述

pipe()函数创建一个管道和指向该管道的一对文件描述符,并且将文件描述符存储到文件描述符数组filedes[]中。其中filedes[0]为读端,filedes[1]为写端。

 

返回值

0 – 管道创建成功;

-1 – 管道创建失败,同时errno置位;

错误指示

EFAULT – 无效的输入参数filedes;

EMFILE – 达到当前进程允许的文件描述符最大值;

ENFILE – 达到系统允许的打开文件的最大数;

实例

下边的例子首先创建一个管道,然后通过fork()创建当先进程的子进程。接着每个进程关闭读写管道不需要的文件描述符。子进程在当前路径下执行“ls –a”命令,通过将管道写描述符fd[1]复制成标准输出,将命令执行输出写到管道;父进程通过fd[0]读取管道数据并显示。


[cpp]  view plain  copy
  1. #include <sys/wait.h>#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>int main(int argc, char *argv[]){    int fd[2];  
  2.   
  3.     pid_t pid;  
  4.   
  5.     char read_buffer[500] = {0};  
  6.   
  7.     int read_count = 0;  
  8.   
  9.     int status = 0;  
  10.   
  11.     //创建管道  
  12.   
  13.     if (pipe(fd) < 0)  
  14.   
  15.     {  
  16.   
  17.         printf("Create pipe failed.");  
  18.   
  19.         return -1;  
  20.   
  21.     }  
  22.   
  23.     //创建子进程  
  24.   
  25.     if ((pid = fork()) < 0)  
  26.   
  27.     {  
  28.   
  29.         printf("Fork failed.");  
  30.   
  31.         return -1;  
  32.   
  33.     }  
  34.   
  35.     //子进程操作  
  36.   
  37.     if (pid == 0)  
  38.   
  39.     {  
  40.   
  41.         printf("[child]Close read endpoint...");  
  42.   
  43.         close(fd[0]);   /* 关闭不使用的读 文件描述符 */  
  44.   
  45.         //复制fd[1]到标准输出  
  46.   
  47.         if (fd[1] != STDOUT_FILENO)  
  48.   
  49.         {  
  50.   
  51.             if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)  
  52.   
  53.             {  
  54.   
  55.                 return -1;  
  56.   
  57.             }  
  58.   
  59.             //close fd[1],标准输出即为fd[1]  
  60.   
  61.             close(fd[1]);  
  62.   
  63.         }  
  64.   
  65.         //执行命令  
  66.   
  67.         status = system("ls –a");  
  68.   
  69.         if (status == -1)  
  70.   
  71.         {  
  72.   
  73.             return -1;  
  74.   
  75.         }  
  76.   
  77.     }  
  78.   
  79.     else  
  80.   
  81.     {  
  82.   
  83.         printf("[parent]Close write endpoint...");  
  84.   
  85.         //父进程 读 操作  
  86.   
  87.         close(fd[1]);   /* 关闭不使用的写 文件描述符 */  
  88.   
  89.         //从管道读数据  
  90.   
  91.         read_count = read(fd[0], read_buffer, 500);  
  92.   
  93.         printf("Content under current directory: \n%s", read_buffer);  
  94.   
  95.     }  
  96.   
  97. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值