C signal handling


In the C Standard Library, signal processing defines how a program handles various signals while it executes. A signal can report some exceptional behavior within the program, or a signal can report some asynchronous event outside the program.

Standard signals

The C standard defines only 6 signals. They are all defined in signal.h header :
Additional signals may be specified in the signal.h header by the implementation. For example, Unix and Unix-like operating systems define more than 15 additional signals; see Unix signal.

Handling

A signal can be generated by calling raise or kill system calls. raise sends a signal to the current process, kill sends a signal to a specific process.
A signal handler is a function which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp.
Signal handlers can be set be with signal or sigaction. The behavior of signal has been changed multiple times across history and is now considered deprecated. It is only portable when used to set a signal's disposition to SIG_DFL or SIG_IGN. Signal handlers can be specified for all but two signals.
If the signal reports an error within the program, the signal handler can terminate by calling abort, exit, or longjmp.

Functions

FunctionDescription
artificially raises a signal
sets the action taken when the program receives a specific signal

Example usage


  1. include
  2. include
  3. include
static void catch_function
int main