Preventing SIGPIPE

余生颓废 提交于 2019-12-23 16:23:26

问题


Let's consider the following example.

I have a parent process that creates a pipe, spawns a child and read the child's standard output using this pipe. At some point, the parent process is no longer interested in the child's output, and closes the read end of the pipe.

Apparently, if the child keeps writing, will this result in the child process receiving a SIGPIPE signal.

Question: is there a way to redirect the child's output to /dev/null so that it still keeps running and producing output, but the parent process can go on doing other things and call waitpid on the child later? Note that setting the child's SIGPIPE handler to SIG_IGN is not an option because I don't have control over the child's signal handlers.


回答1:


Emulate /dev/null yourself -- fork() a new process whose job is to read and ignore the output from the child.




回答2:


Note that setting the child's SIGPIPE handler to SIG_IGN is not an option because I don't have control over the child's signal handlers.

Wrap it in a simple binary. (Even a shellscript will do btw.)




回答3:


Sigpipe happens when the pipe between two component breaks.

You can ignore the sigpipe by the following

signal(SIGPIPE, SIG_IGN);



来源:https://stackoverflow.com/questions/11302873/preventing-sigpipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!