进程之间通过管道来进行通讯是一种常用的方法,顾名思义,管道就是一端进(写)一端出(读)的FIFO队列,这个队列由内核管理,有一定大小(一般是4k),有文章上提到,如果需要修改该缓存区,需要重新编译内核(修改linux/limits.h里PIPE_BUF的定义)。
需要明确的是,虽然管道在理论上是双向的,但实际应用时,为避免复杂,都单向来用,需要双向通讯时,采用两个管道进行。
具有亲缘关系的进程可以采用匿名管道,没有亲缘关系的进程采用命名管道,本文以命名管道为主进行讨论。
创建管道
int mkfifo(const char * pathname,mode_t mode);
打开管道
int open(const char *pathname,int oflag,... /* mode_t mode */);
读写
int read(int handle, void *buf, int nbyte)
int write(int handle, void *buf, int nbyte)
关闭
Close(int handle)
这些函数都比较简单,不做深入说明。
同文件、网络等一样,通过在open方法中是否设置O_NONBLOCK标志,管道操作有两种运行方式:阻塞和非阻塞模式。
阻塞模式下,无论是读端或写端,如果对方没有打开,open都会阻塞,而write和read的行为见表1。该表总结的场景是读端和写端都已顺利打开,双方进程调用write或read发生的情况。
表1 阻塞模式下write和read行为
写端进程 |
读端进程 |
||
关闭 |
管道中还有数据 |
可继续读取 |
未关闭 |
管道中没有数据 |
不阻塞,返回0 |
||
未关闭 |
管道中没有数据 |
阻塞 |
|
管道写满,阻塞 |
|
||
写数据,收到信号SIGPIPE,导致进程异常终止 |
|
关闭 |
无论是读端或写端,open不会阻塞。在双方都顺利打开后,即使没有数据,read不会阻塞,返回-1,errno置为11。write也不会阻塞。
下面代码分读端和写端,属于两个进程,分别编译运行。写端写十次后退出,读端如果读到的数据是“exit",退出。
写数据端
#include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <errno.h> #define FIFO_NAME "mynamedpipe" int main() { int fifo_fd; int num; char buf[1024]; strcpy(buf, "I am coming"); if(access(FIFO_NAME, F_OK)== -1) { fifo_fd = mkfifo(FIFO_NAME, 0777); } printf("Trying to opening named pipe for writing.\n"); fifo_fd = open(FIFO_NAME, O_WRONLY); printf("Opened named pipe for writing.\n"); int i; for(i=0;i<10;i++) { fgets(buf, 100, stdin); num = strlen(buf); num=write(fifo_fd, buf, num); if(num == -1) printf("Error[%d] when writing data into named pipe",errno); else printf("Writed %d chars into named pipe:%s\n", num, buf); } close(fifo_fd); return 0; }
读数据端
#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #define FIFO_NAME "mynamedpipe" int main() { int fifo_fd; int num; char buf[100]; if(access(FIFO_NAME, F_OK)== -1) { fifo_fd = mkfifo(FIFO_NAME, 0777); } printf("Trying to open named pipe for reading... \n"); fifo_fd = open(FIFO_NAME, O_RDONLY); printf("Opened named pipe for reading. \n"); while(1) { num=read(fifo_fd,buf,10); if(num == -1) { printf("Error[%d] when reading data into named pipe",errno); } else { buf[num] = '\0'; printf("Readed %d chars from pipe:%s\n", num, buf); buf[4]= '\0'; if(strcasecmp(buf,"exit")==0) break; } } close(fifo_fd); return 0; }