预防SIGPIPE [英] Preventing SIGPIPE
问题描述
让我们考虑以下示例.
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.
显然,如果子进程继续书写,这将导致子进程接收到SIGPIPE
信号.
Apparently, if the child keeps writing, will this result in the child process receiving a SIGPIPE
signal.
问题:是否有一种方法可以将子级的输出重定向到/dev/null
,以便它仍然保持运行并产生输出,但是父进程可以继续执行其他操作并稍后在子级上调用waitpid
?请注意,将子项的SIGPIPE
处理程序设置为SIG_IGN
不是一种选择,因为我无法控制子项的信号处理程序.
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.
推荐答案
自己模拟/dev/null
-fork()
一个新进程,其工作是读取和忽略子级的输出.
Emulate /dev/null
yourself -- fork()
a new process whose job is to read and ignore the output from the child.
这篇关于预防SIGPIPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!