Signals in operating systems
Signal is kind of a message sent from kernel or process to one or more processes to notify some event occurrence. Every signal has associated number with it.
Kernel generates signals in cases like encountering illegal instruction or file size limit exceeded or I/O is ready. Example of process sending signal to another process are a child process sending signal to parent to indicate completion of execution.
A process will receive a signal when it is running in user mode. If the receiving process is running in kernel mode, the execution of the signal will start only after the process returns to user mode.
When a signal is received by a process :
1. Process can ignore the signal altogether and it does nothing.
2. Process can accept the signal and appropriate signal handler is executed.
Signal handler can be a default handler or it can be user-defined. When a user-defined handler is used for disposition of a signal, signal is said to be caught.
Note that SIGSTOP and SIGKILL can not be caught or ignore. They will always have default action.
When a signal is accepted, process immediately stops current execution and services the signal.
Once the signal handler is completed, process is resumed again. 0-31 is reserved range for standard signals while range 32 to 64 is used by real time signals. To show all the signals which are predefined in a system use command kill -l
Signals and their meaning
This information is taken form man page of signal(7)
Changing the signal handler
signal() function provides the basic functionality for defining signal action for a particular signal. It has two parameters, first is signal number and second is handler we want to have for that signal
sighandler_t signal (int signum, sighandler_t action)
Action can be SIG_IGN or SIG_DFL for ignoring the signal or using default action for signal.
If handler is provided, that handler will be invoked when signal is received.
Other function which can also be used is sigaction(). It give more control over how the handle should be invoked. Prototype is
int sigaction (int signum, const struct sigaction *restrict action,
struct sigaction *restrict old-action)
struct sigaction contains following members
sighandler_t sa_handler : Its is same as in signal function.
sigset_t sa_mask : It indicates set of signals which should be blocked when signal handled is invoked. Note that whenever a signal handler is invoked for a signal, that signal is automatically gets blocked.
int sa_flags : It contains various flags which define the behavior of signal. It is interpreted as bit-mask.
SA_NOCLDSTOP : If it is set, kernel does not send signal for child processes which are stopped.
SA_ONSTACK : Use a signal stack to send this signal
SA_RESTART : It decide what should happen to primitive library calls like read(), write() when signal handler for this signal returns normally. If it is set, library functions resume, if reset, library function calls fail.
signal function is a feature of ISO C, while
sigaction is part of the POSIX.1 standard.
Below is code for changing the handler of a signal