The Channel of IPC
IPC (Inter-Process Communication) is used by Linux in the following ways:
- (1) Pipe and anonymous pipe (FIFO)
(2) Signal
- (3) Message queue
- (4) Shared memory
- (5) semaphores
- (6) socket
What's a pipe?
Pipes are the oldest form of interprocess communication in Unix. We connect a stream of data from one process to another as a "pipe."
Pipes are half-duplex, and data can only flow in one direction; when communication between two parties is required, two pipes need to be established.
Can only be used for communication between parent-child processes or sibling processes (processes that are related).
pipe function #include int pipe(int pipefd[2]);
Function: Create unnamed pipe
Parameters: File descriptor group. fd[0] represents the read end, fd[1] represents the write end.
Return value: 0 for success, error code for failure
In other words, pipe() before fork() can establish a pipeline between the parent-child process and draw a diagram:
Each parent-child process opens five file descriptors, except for the default 0, 1, and 2. Fd[0], fd[1]. Test it and you'll know that these two file descriptors are 3 and 4.
Write string code with one use:
#include #include #include #include int main(){int fd[2];int retByte;pid_t pid;char buf[20] = "";pipe(fd); /* Create unnamed pipe *//printf("%d,%d\n",fd[0],fd[1]);pid = fork();if(pid == -1){ perror("create fork"); return -1;}if(pid == 0){ //child process, write side, use fd[1] //close(fd[0]); //close(fd[1]); while(1) { scanf("%s",buf); if( write(fd[1],buf,strlen(buf)) == -1) { perror("write"); return -1; } memset(buf,0,20); if(read(fd[0],buf,5) > 0 ) { printf("child-read msg: %s\n",buf); } }}else{ //parent process, read end, use fd[0] while(1) { memset(buf,0,20); Byrette = read(fd[0],buf,5);//read only 5 at a time if( retByte == -1) { perror("read"); return -1; } if(retByte > 0) { printf("parent-read msg: %s\n",buf); } }}return 0;}
Run Results:
那么,如果没有读端呢?也就是父子进程的fd[0]都关闭了,会有什么现象呢?
void handler(int no){ printf("SIGPIPE.\n");}int main(){ int fd[2]; int retByte; int rlt; pid_t pid; char buf[20] = ""; rlt = pipe(fd); /*创建无名管道*/ //printf("%d,%d\n",fd[0],fd[1]); signal(SIGPIPE,handler); if(rlt != 0) { perror("pipe"); return -1; } pid = fork(); if(pid == -1) { perror("create fork"); return -1; } if(pid == 0) { //子进程,写端,使用fd[1] close(fd[0]); //close(fd[1]); while(1) { scanf("%s",buf); if( write(fd[1],buf,strlen(buf)) == -1) { perror("write"); return -1; } memset(buf,0,20); } } else { //父进程,读端,使用fd[0] close(fd[0]); while(1) { } } return 0;}
运行结果:
会出现管道破裂!!(如果没有重写管道破裂的处理函数,系统默认的处理方式就是杀死进程,父子进程都over了)
管道读写规则
所以,总结一下读写规则:
读规则:
1)缓冲区没数据:阻塞
2)缓冲区的数据少于请求字节数:缓冲区有多少就读多少
3)缓冲区的数据多于请求字节数:只读取请求字节数,剩下的还在缓冲区
4)写端关闭:读端等待。
写规则:
1)缓冲区满了:写不进去
2)没有读端:管道破裂,父子进程都结束了。调试到write,发生SIGPIPE。
注意:读端和写端的对应关系可以是一对一、一对多、多对一、多对多的。
练习
上面讲到,缓冲区如果满了,就写不进去了。那么缓冲区有多大呢?换言之,如何检测linux中管道的容量?
代码如下:
int main(){int fd[2];pipe(fd);char buf[4096]; //4kint i,loop,ret;for(i = 0 ; i < sizeof(buf) ; i++){ buf[i] = 'a';}loop = 100 ; //如果循环结束,还没阻塞,增加循环次数for(i = 0; i < loop ; i++){ ret = write(fd[1],buf,sizeof(buf)); if(ret == -1) { perror("write error!\n"); return 1; } else { printf("write successfully! "); printf("size: %d K\n", (i+1)*4); }}close(fd[0]);close(fd[1]);return 0;}
运行结果:
在写完64k的时候出现阻塞,说明管道已经满了。