TOC   Previous   Next

pselect(2)

The "self-pipe" technique

Traditional technique to safely wait for file descriptors and a signal:

  1. Create a pipe, mark both ends non-blocking (O_NONBLOCK).

  2. Install handler for signal; when invoked, the handler writes a byte to the pipe.

  3. Include the read end of pipe in set of descriptors monitored by select(2) (or ppoll(2)).

  4. If select(2) says read end of pipe is ready, then we know a signal was delivered.

  5. Drain the pipe, by reading everything from it (multiple signals may occur!), and then perform actions to respond to the signal.

Works, and is portable, but complex.

pselect(2) is simpler (likewise, ppoll(2) and epoll_pwait(2)).


(C) 2006, Michael Kerrisk