Signal handling

suggest change

ISO C declares signal, raise, and the standard signal macros in <signal.h>:

void (*signal(int signal_number, void (*handler)(int)))(int);

The standard signal macros are SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, and SIGTERM. An implementation can define additional signals.

A handler is SIG_DFL, SIG_IGN, or a function with a compatible signature:

void handler(int signal_number);

Portable handler restrictions

An asynchronously invoked handler executes under severe restrictions. Portable handlers usually do no more than assign to an object declared volatile sig_atomic_t and return:

#include <signal.h>

static volatile sig_atomic_t stop_requested;

static void request_stop(int signal_number)
{
    (void)signal_number;
    stop_requested = 1;
}

Do not call printf, malloc, free, or most other library functions from such a handler. ISO C permits only a small set of library calls in this context, including abort, _Exit, quick_exit, limited lock-free atomic operations, and signal for the signal currently being handled. The exact list depends on the language version.

Returning from a handler for SIGFPE, SIGILL, SIGSEGV, or another signal caused by a computational exception has undefined behavior. A handler invoked by abort must not return.

These are ISO C rules. Operating systems may define richer signal APIs and a larger async-signal-safe function set. POSIX, for example, provides sigaction; its guarantees must not be attributed to portable C.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents