问题
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