zoukankan      html  css  js  c++  java
  • 匿名管道和pipe函数

    一、进程间通信

    每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把数据读走,内核提供的这种机制称为进程间通信(IPC,InterProcess Communication)。如下图所示。


    二、管道是一种最基本的IPC机制,由pipe函数创建:
    #include <unistd.h>

    int pipe(int filedes[2]);


    调用pipe函数时在内核中开辟一块缓冲区(称为管道)用于通信,它有一个读端一个写端,然后通过filedes参数传出给用户程序两个文件描述符,filedes[0]指向管道的读端,filedes[1]指向管道的写端(很好记,就像0是标准输入1是标准输出一样)。所以管道在用户程序看起来就像一个打开的文件,通过read(filedes[0]);或者write(filedes[1]);向这个文件读写数据其实是在读写内核缓冲区。pipe函数调用成功返回0,调用失败返回-1。

    开辟了管道之后如何实现两个进程间的通信呢?比如可以按下面的步骤通信。



    示例程序如下:

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    /*************************************************************************
        > File Name: process_.c
        > Author: Simba
        > Mail: dameng34@163.com
        > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
     ************************************************************************/
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    #include<string.h>
    #include<signal.h>
    #define ERR_EXIT(m) 
        do { 
            perror(m); 
            exit(EXIT_FAILURE); 
        } while(0)

    int main(int argc, char *argv[])
    {
        int pipefd[2];
        if (pipe(pipefd) == -1)
            ERR_EXIT("pipe error");

        pid_t pid;
        pid = fork();
        if (pid == -1)
            ERR_EXIT("fork error");

        if (pid == 0)
        {
            close(pipefd[0]);
            write(pipefd[1], "hello", 5);
            close(pipefd[1]);
            exit(EXIT_SUCCESS);
        }

        close(pipefd[1]);
        char buf[10] = {0};
        read(pipefd[0], buf, 10);
        printf("buf=%s ", buf);

        return 0;
    }

    1. 父进程调用pipe开辟管道,得到两个文件描述符指向管道的两端。
    2. 父进程调用fork创建子进程,那么子进程也有两个文件描述符指向同一管道。
    3. 父进程关闭管道写端,子进程关闭管道读端。子进程可以往管道里写,父进程可以从管道里读,管道是用环形队列实现的,数据从写端流入从读端流出,这样就实现了进程间通信。


    三、利用pipe和dup2函数模拟命令行 ls  | wc -w 功能

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    /*************************************************************************
        > File Name: process_.c
        > Author: Simba
        > Mail: dameng34@163.com
        > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
     ************************************************************************/
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    #include<string.h>
    #include<signal.h>
    #define ERR_EXIT(m) 
        do { 
            perror(m); 
            exit(EXIT_FAILURE); 
        } while(0)

    int main(int argc, char *argv[])
    {
        int pipefd[2];
        if (pipe(pipefd) == -1)
            ERR_EXIT("pipe error");

        pid_t pid;
        pid = fork();
        if (pid == -1)
            ERR_EXIT("fork error");

        if (pid == 0)
        {
            dup2(pipefd[1], STDOUT_FILENO); //输出重定向
            close(pipefd[1]);
            close(pipefd[0]);
            execlp("ls", "ls", NULL);
            fprintf(stderr, "error execute ls ");
            exit(EXIT_FAILURE);
        }

        dup2(pipefd[0], STDIN_FILENO);
        close(pipefd[0]);
        close(pipefd[1]);
        execlp("wc", "wc", "-w", NULL);
        fprintf(stderr, "error execute wc ");
        exit(EXIT_FAILURE);

        return 0;
    }


    我们知道命令行 ls | wc -w 中 ls 会输出到管道,而wc 从管道里读取,现在使用dup2复制文件描述符,使ls 的标准输出为管道,wc 的标准输入也为管道,即使父进程先被调度,因为默认是阻塞I/O操作,故wc 会read 阻塞直到管道被子进程写入了数据。


    使用管道有一些限制:

    两个进程通过一个管道只能实现单向通信,比如最上面的例子,父进程读子进程写,如果有时候也需要子进程读父进程写,就必须另开一个管道。

    管道的读写端通过打开的文件描述符来传递,因此要通信的两个进程必须从它们的公共祖先那里继承管道文件描述符。上面的例子是父进程把文件描述符传给子进程之后父子进程之间通信,也可以父进程fork两次,把文件描述符传给两个子进程,然后两个子进程之间通信,总之需要通过fork传递文件描述符使两个进程都能访问同一管道,它们才能通信。


    参考:《APUE》、《linux c 编程一站式学习》

  • 相关阅读:
    leetcode 13. Roman to Integer
    python 判断是否为有效域名
    leetcode 169. Majority Element
    leetcode 733. Flood Fill
    最大信息系数——检测变量之间非线性相关性
    leetcode 453. Minimum Moves to Equal Array Elements
    leetcode 492. Construct the Rectangle
    leetcode 598. Range Addition II
    leetcode 349. Intersection of Two Arrays
    leetcode 171. Excel Sheet Column Number
  • 原文地址:https://www.cnblogs.com/alantu2018/p/8477323.html
Copyright © 2011-2022 走看看