> 文章列表 > 进程间通信 - 管道

进程间通信 - 管道

进程间通信 - 管道

  现今常用的进程间通信方式有:

  • 管道(使用最简单)
  • 信号(开销最小)
  • 共享内存(无血缘关系)
  • 套接字(最稳定)

管道的概念

  这里指匿名管道,管道是一种最基本的IPC机制,作用于有血缘关系的进程之间,比如父子、兄弟、叔侄进程,完成数据传递。

  调用pipe()系统函数即可创建一个匿名管道。其特质如下:

  • 其本质是一个伪文件(实际为内核缓冲区);
  • 由两个文件描述符引用,一个表示读端,一个表示写端;
  • 规定数据从管道的写端流入管道,从读端流出。

管道的局限性

  管道的实现原理:管道实为内核使用环形队列机制,借助内核缓冲区(4K)实现。

  管道的局限性:

  • 数据不能进程自己写,自己读
  • 管道中数据不可反复读取,一旦读走,管道中不再存在;
  • 采用半双工通信方式,数据只能在单方向上流动;
  • 只能在有公共祖先的进程之间使用管道。

pipe函数

  pipe函数,创建并打开管道。

#include <unistd.h>
int pipe(int fildes[2]);参数:fildes[0] - 读端;fildes[1] - 写端;
返回值:0 - 成功;-1 - 失败,errno指使错误;

示例1

  利用管道,实现父进程循环写,子进程循环读。

#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <string.h>int main(int argc, char* argv[])
{int ret = 0;int fd[2];pid_t pid;char buf[256] = {'\\0'};ret = pipe(fd);if (ret == -1){perror("pipe failed!\\n");return -1;}pid = fork();if (pid > 0) {close(fd[0]);     // 关闭读端int i = 0;while (i < 5) {sprintf(buf, "father - %d\\n", i++);write(fd[1], buf, strlen(buf));sleep(5);}close(fd[1]);int status;wait(&status);}else if (pid == 0) {close(fd[1]);     // 关闭写端while (1){ret = read(fd[0], buf, sizeof(buf)-1);if (ret > 0) {write(STDOUT_FILENO, buf, ret);}else {printf("break, ret = %d\\n", ret);break;}}close(fd[0]);}return 0;
}

管道的读写行为

  使用管道注意以下4种情况(假设都是阻塞I/O操作,没有设置O_NONBLOCK标志):

  • 如果所有指向管道写端的文件描述符都关闭了(管道写段引用计数为0),而仍有进程从管道的读端读数据,那么管道中剩余的数据都被读取后,再次read会返回0,就像读到文件末尾一样。
  • 如果有指向管道写端的文件描述符没关闭(管道写端的引用计数大于0),而持有管道写端的进程也没有向管道中写数据,这时有进程从管道读端读数据,那么管道中剩余的数据都被读取后,再次read会阻塞,直到管道中有数据可读了才读取数据并返回。
  • 如果所有指向管道读端的文件描述符都关闭了(管道读端引用计数为0),这时有进程向管道的写端write,那么该进程会收到信号SIGPIPE,通常会导致进程异常终止。当然也可以对SIGPIPE信号实施捕捉,不终止进程。
  • 如果有指向管道读端的文件描述符都没有关闭(管道读端引用计数大于0),而持有管道读端的进程也没有从管道中读数据。这时有进程向管道写端写数据,那么在管道被写满时再次write会阻塞,直到管道中有空位置了才写入数据并返回。

总结:

读管道:

  1. 管道中有数据,read返回实际读到的字节数;

  2. 管道中无数据:

    (1) 管道写端被全部关闭,read返回0;

    (2) 写端没有被全部关闭,read阻塞等待,直到有数据写入管道。

写管道:

  1. 管道读端全部被关闭,写管道,进程收到信号SIGPIPE,进程异常终止(也可以捕捉SIGPIPE信号,使进程不终止);

  2. 管道读端没有全部关闭:

    (1) 管道已满,write阻塞;

    (2) 管道未满,write将数据写入,并返回实际写入的字节数;

示例2

  测试管道读端都被关闭,引用计数为0时,进程向管道写端write会发生什么。

  父进程子进程都关闭读端,父进程在写端写入数据,最终父进程捕捉到SIGPIPE信号。

#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <string.h>
#include <signal.h>void sigProc(int signum)
{printf("The progress %d catch the signal %d.\\n", getpid(), signum);
}int main(int argc, char* argv[])
{int ret = 0;int fd[2];pid_t pid;char buf[256];ret = pipe(fd);if (ret == -1){perror("pipe failed!\\n");return -1;}pid = fork();if (pid > 0){printf("The father progress is %d.\\n", getpid());signal(SIGPIPE, sigProc);close(fd[0]);sleep(3);                            // 留时间给子进程关闭读端strcpy(buf, "hello world");printf("%s\\n", buf);write(fd[1], buf, strlen(buf));close(fd[1]);int status;pid_t c_pid;c_pid = wait(&status);printf("the process %d end, status is %d.\\n", c_pid, status);}else if (pid == 0){printf("The child progress is %d.\\n", getpid());signal(SIGPIPE, sigProc);close(fd[0]);close(fd[1]);printf("The child process %d close fd[0] and fd[1].\\n", getpid());sleep(10);printf("the child progress end.\\n");}return 0;
}

  代码运行结果:

The father progress is 26336.
The child progress is 26337.
The child process 26337 close fd[0] and fd[1].
hello world
The progress 26336 catch the signal 13.
the child progress end.
the process 26337 end, status is 0.