现代进程间的通信方式--管道

管道(pipe)、命名管道(FIFO)、信号(signal)、消息队列、共享内存、信号量、套接字(socket)

--------------------------------------------------------------------------------------

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

/*父进程通过管道传输两个数据给子进程
 *由子进程负责从管道中读取并输出
*/

int main()
{
    int fd[2]; //fd[0] fd[1]
    //创建管道
    if(pipe(fd)<0)
    {
        perror("pipe error");
        exit(1);
    }

    pid_t pid;
    if((pid = fork()) < 0)
    {
        perror("fork error");
        exit(1);
    }
    else if(pid >0)  //parent process
    {
          close(fd[0]);//父进程用来写入数据
          int start =1,end = 100;
          if(write(fd[1],&start,sizeof(int))!=sizeof(int))
          {
            perror("write error");
            exit(1);
          }
          if(write(fd[1],&end,sizeof(int))!=sizeof(int))
          {
            perror("write error");
            exit(1);
          }
          close(fd[1]);
          wait(0);         //等待子进程结束
          
    }
    else   //child process
    {
         close(fd[1]);//子进程用来读取数据
         int start,end;
         if(read(fd[0],&start,sizeof(int)) < 0)
         {
            perror("write error");
            exit(1);
         }
         if(read(fd[0],&end,sizeof(int)) < 0)
         {
            perror("write error");
            exit(1);
         }
         close(fd[0]);
         printf("child process read start:%d,end:%d ",start,end);
    }
    exit(0);
}

-------------------------------------------------------

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

char *cmd1[3] = {"/bin/cat", "/etc/passwd", NULL};
char *cmd2[3] = {"/bin/grep", "root", NULL};

int main(void)
{
    int fd[2];
    if(pipe(fd)<0)
    {
        perror("pipe error");
        exit(1);
    }
    int i=0;
    pid_t pid;
    for(:i<2;i++)
    {
        pid = fork();
        if(pid < 0)
        {
            perror("fork error");
            exit(1);
        }
        else if(pid == 0) //child process
        {
            if(i==0)  //第一个子进程,负责往管道写入数据
            {        
              close(fd[0]);   //关闭读端   
              
               /*将标准输入重定向到管道的写端*/
              if(dup2(fd[1]),STDIN_FILENO)!= STDIN_FILENO)
              {
                perror("dup2 error");
              }
              close(fd[1]);
              
              //调用exec函数执行cat命令
              if(execvp(cmd1[0],cmd1) <0 )
              {
                perror("execvp error");
              }
              break;
            }
            if(i==1)  //第二个子进程,负责往管道读入数据
            {         
              close(fd[1]);           //关闭写端
                    
              //将标准输出重定向到管道的读端
              if(dup2(fd[0],STDOUT_FILENO)!= STDOUT_FILENO)
              {
                perror("dup2 error");
              }
              close(fd[0]);
              
               //调用exec函数执行grep命令
              if(execvp(cmd2[0],cmd2) <0 )
              {
                perror("execvp error");
                exit(1);
              }
              break;
            }
        }
        else   //parent process
        {
            if(i == 1)
            {
                //父进程要等待子进程全部创建完毕后再回收
                close(fd[0]);
                close(fd[1]);
                wait(0);
                wait(0);
            }
        }    
    }
}


    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    
    
    
    
    
    
    
    
    
    
    
    
    
    

原文地址:https://www.cnblogs.com/lvdh1314/p/6508731.html