Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Clarified the part about POSIX signal masking

A signal handler should not re-assert its desire to handle its own signal. This is often done on non-persistent platforms, that is, when a signal handler receives a signal that is bound to a handler, they these platforms unbind the signal to default behavior before calling the handler.

...

Code Block
bgColor#ccccff
void handler(int signum) {
  /* handling code */
}
/* ... */
/* Equivalent to signal( signum, handler);
   but make signal persistent */
struct sigaction act;
act.sa_handler = &handler;
act.sa_flags = 0;
if (sigemptyset( &act.sa_mask) != 0) {
  /* handle error */
}
if (sigaction(signum, &act, NULL) != 0) {
  /* handle error */
}

While the handler in this example does not call signal(), it safely can, since relevant signals are the signal is masked and so the handler can not be interrupted. Note that if the same handler is installed for more than one signal number, it would be necessary to mask the signals explicitly in act.sa_mask in order to ensure the handler can not be interrupted, since the system only masks the signal being delivered.

In fact, POSIX recommends sigaction(2) and deprecates signal(2). Unfortunately, sigaction(2) is not C99-compliant, and is not supported on some platforms, including Windows.

...