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
jitendra@jitendra-laptop:~$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
Real time signals
34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7
42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11
46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15
50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8
57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4
61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
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.
signalfunction is a feature of ISO C, while
sigactionis part of the POSIX.1 standard.
Below is code for changing the handler of a signal
/* This flag controls termination of the main loop. */
volatile sig_atomic_t cont = 1;
my_signal_handler1 (int sig);
/* This is second handler set in first handler */
my_signal_handler (int sig)
cont = 0;
puts ("Got second alarm....");
signal (sig, my_signal_handler1);
/* The signal handler prints the message and enables alarm signal again with different handler. */
my_signal_handler1 (int sig)
puts ("Got an alarm....");
signal (sig, my_signal_handler);
puts ("Doing stuff while waiting for alarm....");
/* Establish a handler for SIGALRM signals. */
signal (SIGALRM, my_signal_handler1);
/* Set an alarm to go off in a little while. */
/* Check the flag once in a while to see when to quit. */