7.3 sigaction
If your program needs to specify its own handler, the proper method is to use sigaction.
This function has three parameters:
- int signum: the first parameter specifies the signal number that sigaction applies to.
- const struct sigaction *act: the second parameter is a pointer to specify the new handler of the signal.
- struct sigaction *oldact: the third parameter is a pointer to storage to save the current handler. If this
parameter is NULL, then the previous handler is lost.
struct sigaction is a structure that consists of many attributes:
- sa_handler and sa_sigaction should be mutually exclusive.
- sa_mask: This field specifies signals that should be allowed to interrupt when the signal handler is executing.
The functions sigaddset, sigdelset and sigemptyset should be used to specify the mask. If a signal handler
is to block all other signals, then sigemptyset should be used.
- sa_flags: Specify SA_SIGINFO in this field to use sa_sigaction (instead of sa_handler). It can also contain
other flags (bitwise-ored if multiple are specified).
- sa_restorer: this attribute is said to be obsolete and should not be used.
7.3.1 Why the complication?
Compared to signal, sigaction is a bit more complex to use. However, this is not without reasons.
First of all, the sa_mask field of struct sigaction allows the specification of whether the handling of a particular signal
can block other signals (including another instance of itself!) from interrupting it. This is very important when it comes to
variable consistency.