众所周知,在Linux的进程间通信期间,进程通过一个名为"Pipe">的特殊文件相互通信。
众所周知,对该文件执行的操作由一个进程写入并由一个进程读取,以便相互通信。
现在,问题是:
这些写入和读取操作在通信期间是否并行执行(操作并行执行(? 如果不是,
当其中一个进程在通信期间进入 SLEEP 状态时会发生什么情况?它是先执行写入操作以便读取第二个进程,还是直接进入睡眠状态而不执行任何写入和读取操作?
发送过程可以写入,直到管道缓冲区已满(自 2.6.11 起在 Linux 上为 64k(。之后, write(2( 将阻塞。
接收进程将阻塞,直到数据可供读取(2(。
有关管道缓冲的更详细信息,请查看 https://unix.stackexchange.com/a/11954。
例如,该程序
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
char wbuf[32768];
char buf[16384];
/* Initialize writer buffer with 012...89 sequence */
for (int i = 0; i < sizeof(wbuf); i++)
wbuf[i] = '0' + i % 10;
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child reads from pipe */
close(pipefd[1]); /* Close unused write end */
while (read(pipefd[0], &buf, sizeof(buf)) > 0);
close(pipefd[0]);
_exit(EXIT_SUCCESS);
} else { /* Parent writes sequence to pipe */
close(pipefd[0]); /* Close unused read end */
for (int i = 0; i < 5; i++)
write(pipefd[1], wbuf, sizeof(wbuf));
close(pipefd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}
使用gcc pipes.c && strace -e trace=open,close,read,write,pipe,clone -f ./a.out
运行时将产生以下序列:
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "177ELF2113 3 >