What makes a Unix process die with Broken pipe?
来源:互联网 发布:淘宝宝贝模块代码 编辑:程序博客网 时间:2024/06/06 10:28
What makes a Unix process die with Broken pipe?
Here are some options I thought of, not sure which is the right one.
a) There was an I/O error reading from the pipe.
b) The process writing to the other end of the pipe died with a failure.
c) All processes who could write to the pipe have closed it.
d) The write buffer of the pipe is full.
e) The peer has closed the other direction of the duplex pipe.
f) Writing failed because there are no processes which could read from the pipe.
g) A system call returned the EPIPE error, and there was no error handler installed.
3 Answers
A process receives a SIGPIPE when it attempts to write to a pipe (named or not) or socket of type SOCK_STREAM that has no reader left.
It's generally wanted behaviour. A typical example is:
find . | head -n 1
You don't want find
to keep on running once head
has terminated (and then closed the only file descriptor open for reading on that pipe).
The yes
command typically relies on that signal to terminate.
yes | some-command
Will write "y" until some-command terminates.
Note that it's not only when commands exit, it's when all the reader have closed their reading fd to the pipe. In:
yes | ( sleep 1; exec <&-; ps -fC yes) 1 2 1 0
Their will be 1 (the subshell), then 2 (subshell + sleep), then 1 (subshell) then 0 fd reading from the pipe after the subshell explicitely closes its stdin, and that's when yes
will receive a SIGPIPE.
Above, most shells use a pipe(2)
while ksh93
uses a socketpair(2)
, but the behaviour is the same in that regard.
When a process ignores the SIGPIPE, the writing system call (generally write
, but could be pwrite
, send
, splice
...) returns with a EPIPE
error. So processes wanting to handle the broken pipe manually would typically ignore SIGPIPE and take action upon a EPIPE error.
(f)
Writing failed because there are no processes which could read from the pipe.
Although unless you duplicate descriptors and fork, there can only be one process to start with: generally a pipe has one reader and one writer, and when one of them closes the connection, the pipe is defunct. If you are using a named pipe, you can can make multiple connections (in serial) with it, but each one represents a new pipe in this sense. So a "pipe" to a thread or process is synonymous with a file descriptor.
From man 7 pipe
:
If all file descriptors referring to the read end of a pipe have been closed, then a write(2) will cause a SIGPIPE signal to be generated for the calling process. If the calling process is ignoring this signal, then write(2) fails with the error EPIPE.
So a "broken pipe" is to the writer what EOF is to the reader.
yes
command relies on SIGPIPE to terminate, the head
command generally causes SIGPIPEs to be sent. – Stéphane Chazelas Jul 29 '13 at 16:27A broken pipe happens when the reading process exits before the writing process. So I would go with (f)
- What makes a Unix process die with Broken pipe?
- What Makes a Good Teacher?
- What Makes a Good Teacher?
- What makes a good programmer
- What makes a great team
- What Makes A Great Programmer?
- What makes a good leader
- What Makes A Great Programmer?
- What Makes a Good Utility?
- howto prevent a process from terminating when writing to a broken pipe
- What are various IDs associated with a process?
- What makes a good software manager?
- What makes a good Software Test engineer?
- What Makes a Good Integration Developer?
- What makes a good team?(BEC)
- What Makes a Good Developer Culture?
- What Makes a Good Programmer Good?
- What makes a great web developer?
- git hub第一次使用出现问题解决
- 2015 湘潭大学程序设计比赛(Internet)
- percona xtradb cluster安装步骤
- Android网络编程之Socket通信
- jQuery显示和隐藏 常用的状态判断方法
- What makes a Unix process die with Broken pipe?
- 验证码无法显示:Could not initialize class sun.awt.X1 的解决方案
- 李克强会见饶宗颐先生
- 理清mysql外链的含义和命令
- 花呗套现防骗说明
- 行人检测 最新算法
- JQuery之ContextMenu(右键菜单)
- 大家有哪些好的android开发习惯?
- [G+smo]gsFunction