开发者社区> lin.tao> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

What makes a Unix process die with Broken pipe?

简介: What makes a Unix process die with Broken pipe? up vote6down votefavorite Here are some options I thought of, not sure which is the right one. a) The
+关注继续查看

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.

shareimprove this question
 
 
What's your question? Are you asking which of those is correct, or if there are any other things that can cause the broken pipe? –  EightBitTony Jul 29 '13 at 15:15
 
@EightBitTony Which of these is correct –  siamii Jul 29 '13 at 15:16

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 pwritesendsplice...) 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.

shareimprove this answer
 

(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.

shareimprove this answer
 
4  
It's not always a mistake. The yes command relies on SIGPIPE to terminate, the head command generally causes SIGPIPEs to be sent. –  Stéphane Chazelas Jul 29 '13 at 16:27

A broken pipe happens when the reading process exits before the writing process. So I would go with (f)

shareimprove this answer
 
 
There can be several processes reading or writing to a pipe, and the same process can be reading and writing. Also, it's not about exiting, it's about closing the file descriptor. –  Stéphane Chazelas Jul 29 '13 at 16:26

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
BrokenPipeError: [Errno 32] Broken pipe解决方法
BrokenPipeError: [Errno 32] Broken pipe解决方法
0 0
(转) 解决ssh的"Write failed: Broken pipe"问题
解决ssh的"Write failed: Broken pipe"问题   问题场景 服务器环境:阿里云 Linux CentOS 主机 客户端:Mac OSX Terminal 问题现象 用 ssh 命令连接服务器之后,如果一段时间不操作,再次进入 Terminal 时会有一段时间没有响应,然后就出现错误提示: Write failed: Broken pipe 只能重新用 ssh 命令进行连接。
1824 0
Error_Unix Shell_syntax error near unexpected token `fi'
2014-06-19 BaoXinjian 1. Issue 调用如下Shell脚本时出现错误syntax error near unexpected token `fi'   2. Cause Unix 和 Window存放文件时其编码方式是不一致的,导致Window写好的文件放到Unix中,会有部分乱码出现,主要是换行阶段   3.
828 0
A Unix Utility You Should Know About: Pipe Viewer
Hi all. I'm starting yet another article series here. This one is going to be about Unix utilities that you should know about.
566 0
A Unix Utility You Should Know About: lsof
This is the third post in the article series about Unix and Linux utilities that you should know about.
479 0
A Unix Utility You Should Know About: Netcat
This is the second post in the article series about Unix utilities that you should know about.
581 0
+关注
lin.tao
无他,唯手熟尔
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载