关于linux下pipe管道的用法

管道主要用于进程之间的通信,而pipe管道主要用于具有亲戚关系的进程之间进行通信,也叫匿名管道。以下是几个测试demo

1,通过主进程作媒介来实现两个子进程之间的通信

思路:主进程与两个子进程之间分别建立管道,一个用于从子进程输入数据到主进程,另一个管道用于从子进程输出数据到另一个子进程,代码如下:

#include <unistd.h>
#include <stdio.h>
#include<stdlib.h>
#include<wait.h>
#include <iostream>
#include <thread>

int main()
{
    int pipefd1[2],pipefd2[2];
    char buf[1024];

    if (pipe(pipefd1) == -1||pipe(pipefd2) == -1)
        {
        perror(" Web pipe error:");
        exit(EXIT_FAILURE);
    }
   pid_t childA = fork();//创建一个子进程,用于写
   pid_t childB = fork();//创建一个子进程,用于读
   if (childA ==-1 || childB ==-1)
       {
       perror("fork  error:");
        exit(EXIT_FAILURE);
   }
   else if (childB==0)
       {
       if((close(pipefd1[1]))==-1)//子进程只读
        {
         perror("close write  error:");
         exit(EXIT_FAILURE);
        }
       while(1)
       { //循环读取数据
         read(pipefd1[0],buf,1024);//最多读取1024个字节
         printf("childB read from pipe :  %s\n",buf);
       }
      }
   else if (childA>0 && childB>0){
       // 只写
       if((close(pipefd1[0]))==-1){
        perror("close read error:");
        exit(EXIT_FAILURE);
       }
       if((close(pipefd2[1]))==-1){
        perror("close read error:");
        exit(EXIT_FAILURE);
       }
      while(1)//循环写入内容
   {
     waitpid(childA,NULL,WNOHANG);//等待子进程退出
     waitpid(childB,NULL,WNOHANG);

      read(pipefd2[0],buf,1024);
      printf("mainProcess read from pipe :  %s\n",buf);
      write(pipefd1[1],buf,strlen(buf)+1);//具体写多少个字节
      printf("mainProcess write to pipe :  %s\n",buf);
      }
    }

   else if (childA == 0)
       {
       if((close(pipefd2[0]))==-1)//子进程只写
        {
         perror("childA close read error:");
         exit(EXIT_FAILURE);
        }
       while(1)
           {
           std::cin>>buf;
           write(pipefd2[1],buf,strlen(buf)+1);//具体写多少个字节
           printf("childA write to pipe :  %s\n",buf);
       }
   }
   return 1;
}

终端输入输出信息如下:

2,两个子进程通过pipe直接传输数据,不通过主进程

#include <unistd.h>
#include <stdio.h>
#include<stdlib.h>
#include<wait.h>
#include <iostream>
#include <thread>

int main()
{
    int pipefds[2];
    char buf[1024];
    if (pipe(pipefds) ==-1)
        {
        perror(" Web pipe error:");
        exit(EXIT_FAILURE);
    }
    pid_t childA = fork();//创建一个子进程,用于写
    pid_t childB = fork();//创建一个子进程,用于读
    if (childA == -1 || childB == -1)
        {
        perror("pipe error:");
        exit(EXIT_FAILURE);
    }
    if (childA == 0)
        {
        if((close(pipefds[0]))==-1)//子进程只写
         {
          perror("childA close read error:");
          exit(EXIT_FAILURE);
         }
        while(1)
            {
            std::cin>>buf;
            write(pipefds[1],buf,strlen(buf)+1);//具体写多少个字节
            printf("childA write to pipe :  %s\n",buf);
        }
    }

    if (childB == 0)
        {
        if((close(pipefds[1]))==-1)//子进程只读
         {
          perror("close write  error:");
          exit(EXIT_FAILURE);
         }
        while(1)
        { //循环读取数据
          read(pipefds[0],buf,1024);//最多读取1024个字节
          printf("childB read from pipe :  %s\n",buf);
        }
    }
    if (childA >0 && childB >0)
        {
        while(1)
            {
            // 防止主进程退出
        }
    }
    return 0;
}

终端打印出来的结果:

3,由于pipe是一种半双工的通信方式,如过要是实现两个子进程之前能够同时进行双向传输,实现全双工,必须要建立两个数据流向相反的管道(注:原则上也可通过只创建一个通道,然后close()来改变传输方向来实现双向传输,但一个通道无法实现同时双向传输),双管道传输代码如下:
#include <unistd.h>
#include <stdio.h>
#include<stdlib.h>
#include<wait.h>
#include <iostream>
#include <thread>

 

void writeThread(int fd,char* buf,const char* name)
{
    while(1)
        {
        std::cin>>buf;
        char s[1024];
        sprintf(s,"%s write to pipe:",name);
        write(fd,buf,strlen(buf)+1);//具体写多少个字节
        printf("%s%s\n",s,buf);
    }
}

int main()
{
    int pipefds1[2];
    int pipefds2[2];
    char buf1[1024];
    char buf2[1024];
    if (pipe(pipefds1) ==-1 || pipe(pipefds2) ==-1)
        {
        perror(" pipe error:");
        exit(EXIT_FAILURE);
    }
    pid_t childA = fork();
    pid_t childB = fork();
    if (childA == -1 || childB == -1)
        {
        perror("pipe error:");
        exit(EXIT_FAILURE);
    }
    if (childA == 0)
        {
        if((close(pipefds1[0]))==-1)// 对于管道1,进程A只写
         {
          perror("childA close read error:");
          exit(EXIT_FAILURE);
         }
        if ((close(pipefds2[1]))==-1) // 对于管道2,进程A只读
            {
            perror("childA close write error:");
            exit(EXIT_FAILURE);
        }
        std::thread t(writeThread,pipefds1[1],buf1,"childA");
        t.detach();
        while(1)
            { //循环读取数据
            read(pipefds2[0],buf2,1024);//最多读取1024个字节
            printf("childB read from pipe :  %s\n",buf2);
        }


    }

    if (childB == 0)
        {
        if((close(pipefds1[1]))==-1)//对于管道1,进程B只读
         {
          perror("childB close write  error:");
          exit(EXIT_FAILURE);
         }
        if((close(pipefds2[0]))==-1)//对于管道2,进程A只写
         {
          perror("childB close read error:");
          exit(EXIT_FAILURE);
         }
        std::thread t(writeThread,pipefds2[1],buf2,"childB");
        t.detach();
        while(1)
        { //循环读取数据
          read(pipefds1[0],buf1,1024);//最多读取1024个字节
          printf("childB read from pipe :  %s\n",buf1);
        }
    }
    if (childA >0 && childB >0)
        {
        while(1)
            {
            // 防止主进程退出
            sleep(1);
        }
    }
    return 0;
}

终端输入和输出:

 

注:关于fork函数的用法及作用可参考这个老哥的博客:https://blog.csdn.net/kxjrzyk/article/details/81603049

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值