From my understanding, SIGPIPE can only occur as the result of a write(), which can (and does) return -1 and set errno to EPIPE… So why do we have the extra overhead of a signal? Every time I work with pipes I ignore SIGPIPE and have never felt any pain as a result, am I missing something?
From my understanding, SIGPIPE can only occur as the result of a write() ,
Share
I don’t buy the previously-accepted answer.
SIGPIPEis generated exactly when thewritefails withEPIPE, not beforehand – in fact one safe way to avoidSIGPIPEwithout changing global signal dispositions is to temporarily mask it withpthread_sigmask, perform thewrite, then performsigtimedwait(with zero timeout) to consume any pendingSIGPIPEsignal (which is sent to the calling thread, not the process) before unmasking it again.I believe the reason
SIGPIPEexists is much simpler: establishing sane default behavior for pure “filter” programs that continuously read input, transform it somehow, and write output. WithoutSIGPIPE, unless these programs explicitly handle write errors and immediately exit (which might not be the desired behavior for all write errors, anyway), they will continue running until they run out of input even if their output pipe has been closed. Sure you can duplicate the behavior ofSIGPIPEby explicitly checking forEPIPEand exiting, but the whole purpose ofSIGPIPEwas to achieve this behavior by default when the programmer is lazy.