title: Block until a reader has connected to named pipe
body: <p>I've written a server which opens a named pipe (which blocks, so it waits until a client is connected), and then periodically writes something to the pipe. The client opens the pipe, reads from it and processes the data. However, due to circumstances I can't control, the client frequently exits and is restarted again soon thereafter.</p>
  
  <p>This causes problems when the server wants to write something in the short interval when there is no reader connected to the pipe: the server receives a SIGPIPE and exits. I can ignore the signal, but I don't want to lose the data: ideally, the server would wait until the client has reconnected to the pipe before writing the data. It's no problem for the server to block during this write. </p>
  
  <p>Using <code>write()</code> I can try a 0-byte write and check for a <code>EPIPE</code> error to detect whether there is a client connected. But how can I block until a client is connected (aside from sleeping a bit, and trying the write again)?</p>
  
  <p>Or is there another, better way to achieve this?</p>