How do I wait for an EPIPE on Linux and Unix?
I'm writing a program on Linux in C, and I'd like to wait (using select or poll) for a pipe file descriptor to become broken, e.g. I'd like to wait up to the point when a subsequent write(2) would return EPIPE (or SIGPIPE would be sent). Adding the file descriptor to the writefds or the exceptfds argument of select doesn't solve the problem. I know that I can solve this by creating multiple processes or threads, but in this question I'm only interested in 开发者_如何学Csingle-process, single-thread solutions, preferably involving select and poll (because I want to wait on other file descriptor events as well). I cannot make any assumption about who are on the other end of the pipe -- the solution must work if those processes are far away in the process tree.
I'd prefer a solution which is portable across many Unix systems (not just Linux).
Calling poll(2) with POLLHUP on the write-end of the pipe seems to have done trick on Linux.
精彩评论