0

We need to redirect an application's stdout to our program. And we cannot change the application. So that, we cannot handle SIGPIPE in the writer. We do not want the application terminate when our program crashes. Unnamed pipe cannot work then. But what about named pipe? We are thinking, if our program crashes, we can restart it, and attach it to the named pipe. Does it make any sense?

Will the writer on a named pipe just be blocked in SIGPIPE?

1 Answer 1

1

The writer will get a SIGPIPE when the reader of the named pipe goes away, just as with an unnamed pipe. That proposal won't work directly.

What does work is having a process that holds the named pipe open for reading indefinitely, but never reads it. Your unreliable process can then also open the named pipe, read from it, crash when it must, and a new incarnation can repeat the process, all without sending SIGPIPE to the writer process.

Note that (a) you might get data lost because your reader read it but crashed before processing it, and (b) your write might get blocked on write if the pipe reaches its capacity.

The capacity of a FIFO is system dependent; it may be that you don't run into difficulty because it is big enough. On Linux, the capacity of a FIFO is 64 KiB, for example, determined by running:

mkfifo fifo
sleep 1000 < fifo &  # Do-nothing 'reader' process
dd if=/dev/zero of=fifo bs=1k count=2048

and hitting interrupt; dd reports that it wrote 64 blocks of 1 KiB, hence 64 KiB for the FIFO size. But be cautious; the size will be different on other systems — for example, the same technique on Mac OS X 10.9.4 reports that the capacity is 8 KiB.

But maybe you should consider fixing the unreliable process so it doesn't crash?

1
  • Note that a simple program (I call it pause) can be useful: #include <unistd.h> and int main(void) { pause(); return(0); }. This is pretty much the ultimate do nothing program; the pause() system call does not return until an unignored signal arrives (and even then, it only returns if there's a signal handler in place that catches the signal and returns rather than exiting). I really used pause < fifo & to wait on the FIFO, rather than sleep 1000. Jul 9, 2014 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.