3

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.

  • The suggestions below from Gergely and sarnold are basically correct. But note that SIGPIPE exists for a reason: if you didn't have it, processes that generate output would spin all the way to the end of input (which might be forever) instead of exiting -- tools like "more" or "head/tail" would be impossible to write. Are you really sure this is the behavior you want? – Andy Ross Jul 03 '12 at 04:26

3 Answers3

2

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

sarnold
  • 102,305
  • 22
  • 181
  • 238
1

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

Gergely Szilagyi
  • 3,813
  • 1
  • 14
  • 12
  • 1
    This will prevent the death by SIGPIPE, assuming the child doesn't reset its signal handlers to SIG_DFL. But then it will get an EPIPE error from the write(), which may cause it to complain and exit. – Alan Curry Jul 03 '12 at 00:18
  • .. though I have trouble seeing what exactly one would do with a child that keeps running but is otherwise ignored. :) – sarnold Jul 03 '12 at 00:25
  • 1
    @sarnold: well, in the end you can do a `nohup binary.bin > /dev/null < /dev/zero` in a chrooted environment, preferably under Xen :D It will be the ultimate "don't hear don't see don't speak" program :) – Gergely Szilagyi Jul 03 '12 at 00:29
  • You can set `SIGPIPE` to `SIG_IGN` after the `fork()` returns 0 and before executing the child. Assuming the child does not explicitly reset it, the child will then never receive `SIGPIPE`. (Although its writes will get the `EPIPE` error as Alan mentioned.) – mark4o Jul 16 '12 at 22:34
0

Sigpipe happens when the pipe between two component breaks.

You can ignore the sigpipe by the following

signal(SIGPIPE, SIG_IGN);

Anu
  • 1
  • 1