pipe作为linux中最基础的进程间通信机制,经常在shell中使用,例如ps aux | grep aaa 即建立了一个管道,而linux 下C程序同样可以通过系统调用pipe在父子进程间使用管道功能。
pipe函数原型如下:
#include <unistd.h> int pipe(int pipefd[2]);
- 通过函数参数返回两个描述符(fd),pipefd[0] 用来读,pipefd[1]用来写, 写入到pipefd[1]的数据将可以从pipefd[0]读出,管道作为半双工通道,数据只能沿一个方向前进;
- pipe函数返回0表示调用成功,返回-1表示调用失败;
- 读取一个空的pipe将导致read操作阻塞,直到有数据被写入到pipe中;向一个已经满的pipe写数据将导致write操作阻塞,直到pipe中的数据被读取出去;
如果想避免read和write阻塞,可通过fcntl将pipefd设置成O_NONBLOCK,read和write无论是否成功,都将直接返回,这时需要判断read和write返回错误码,判断操作是否成功。
- 如果有多个进程同时向一个pipe写入时,只有在每个进程写入的数据长度都小于PIPE_BUF时,才可以保证pipe写入的原子性,不然可能会出现数据错乱的情况;
- 管道作为半双工通道,如果想实现双向通信,则需要打开两个管道,一个从父进程->子进程,另一个从子进程->父进程。
#include <unistd.h> void server(int wfd) { write(wfd, "HELLO PIPE\n", 11); } void client(int rfd) { char buffer[128]; int nRead; nRead = read(rfd, buffer, 128); if(nRead > 0) { write(1, buffer, nRead); } } int main(int argc, char **argv) { pid_t cpid; int pipefd[2]; if(pipe(pipefd) < 0) { perror("pipe error:"); return 1; } cpid = fork(); if(cpid < 0) { perror("fork error:"); return 1; } if(cpid == 0) { /*client*/ close(pipefd[1]); /*close the write fd*/ client(pipefd[0]);
close(pipefd[0]); } else { /*server*/ close(pipefd[0]); /*close the read fd*/ server(pipefd[1]);
close(pipefd[1]);
waitpid(cpid, NULL, 0); } return 0; }
以上示例完成了最基本的pipe进程间通信,父进程fork出子进程,子进程会继承父进程打开着的pipefd描述符,子进程关闭pipefd[1],保留pipefd[0]用来写入;
而父进程关闭pipefd[0],保留pipefd[1]用来写入。