4

I am running an epoll loop and sometimes my call to epoll_wait returns -1 with errno set to EINTR. Sometimes, I want this to end the epoll loop, like in the case of SIGTERM or SIGINT. But I have this code compiled with the -pg flag, so periodic SIGPROF (27) signals are raised that stop my loop.

So... is it possible to switch on the signum so that I can determine when to exit vs. continue? I would like to avoid anything that employs the use of a global to keep track of the most recent signal fired.

Robert Ancell
  • 233
  • 3
  • 7
Brian Schlenker
  • 4,966
  • 6
  • 31
  • 44
  • While it still employes a global, an [`eventfd`](http://man7.org/linux/man-pages/man2/eventfd.2.html) or the self-pipe trick can provide a fairly clean way to be notified of a signal via `epoll` and then synchronously process it based on signum. – Tanner Sansbury Nov 11 '13 at 16:23
  • @Tanner How would I go about using this? Make epoll_wait ignore signals, register for eventfd, register signal handler that notifies of signum through the eventfd? – Brian Schlenker Nov 11 '13 at 16:30
  • I mainly use the [self pipe trick](http://man7.org/tlpi/code/online/diff/altio/self_pipe.c.html), but the overall flow is the same with `eventfd`. Add a signal handler that writes to the pipe/fd, register for the fd, wait on I/O notification (continue on `EINTR`), pipe/fd will be ready to be read and selected by I/O, at which point signum can be read and processed. – Tanner Sansbury Nov 11 '13 at 18:41

1 Answers1

1

Add signal handlers on SIGTERM and SIGINT. Inside those handlers you set a variable that you check in your main epoll loop

Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19