且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

读取过程在写入过程之前终止时的Unix/Linux管道行为

更新时间:2023-12-03 20:35:28

如果某些管道(2)关闭( 2) -ed,进一步 write(2)将会得到SIGPIPE signal(7).另请阅读 pipe(7).

If the read end of some pipe(2) is close(2)-ed, further write(2)s will get a SIGPIPE signal(7). Read also pipe(7).

当管道缓冲区已满时,他们将获得SIGPIPE.

yes | ./readstdin命令中,yes命令获得一个SIGPIPE信号.只需在终端中尝试yes,它会无限期地吐出一些恶作剧内容,直到您将其杀死为止.

In the yes | ./readstdin command, the yes command gets a a SIGPIPE signal. Just try yes in a terminal, it spits some output indefinitely ad nauseam till you kill it.

cat file | ./readstdin命令中,可能会发生(特别是如果file很小,小于sysconf(_POSIX_PIPE_BUF)字节,可能是4096字节),则cat命令是close(2)- STDOUT_FILENO描述符,并且管道仍未满.那么cat可能不会得到任何SIGPIPE.

In the cat file | ./readstdin command, it could happen (notably if file is quite small, less that sysconf(_POSIX_PIPE_BUF) bytes, which might be 4096 bytes), that the cat command is close(2)-ing the STDOUT_FILENO descriptor and that the pipe is still not full. Then cat may not get any SIGPIPE.