TOC
Previous
Next
pselect(2)
The "self-pipe" technique
Traditional technique to safely wait for file descriptors and a signal:
-
Create a pipe, mark both ends non-blocking (O_NONBLOCK).
-
Install handler for signal; when invoked, the handler writes a byte to the pipe.
-
Include the read end of pipe in set of descriptors monitored by
select(2) (or
ppoll(2)).
-
If select(2) says read end of pipe is ready,
then we know a signal was delivered.
-
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