预防SIGPIPE [英] Preventing SIGPIPE

查看:86
本文介绍了预防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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆