Andreas Christoforou         home     posts

Understanding Signals

Signals are software interrupts to handle asynchronous events.

A signal may be sent from the kernel to a process, from a process to another process, or from a process to itself, or is sent either to a specific thread (Linux task) or to the process as a whole (Linux thread group).

In Linux, every signal has a name that begins with characters SIG.

When the signal is delivered, either it’s caught or ignored by a user handler or it has a default action.

Ignore - nothing will happen but SIGKILL and SIGSTOP cannot be ignored.
Catch and handle Signal Kernel will suspend execution of the process and run handle code or chose to terminate.
Default action, this could be the default action to terminate the process or ignore.

Signals Identifiers

Signals are defined in signal.h header file.

+--------------------+------------------+
|  POSIX signal      |  default action  |
+--------------------+------------------+
|  SIGHUP            |  terminate       |
|  SIGINT            |	terminate       |
|  SIGQUIT           |	coredump        |
|  SIGILL            |	coredump        |
|  SIGTRAP           |	coredump        |
|  SIGABRT/SIGIOT    |  coredump        |
|  SIGBUS            |	coredump        |
|  SIGFPE            |	coredump        |
|  SIGKILL           |	terminate(+)    |
|  SIGUSR1           |	terminate       |
|  SIGSEGV           |	coredump        |
|  SIGUSR2           |	terminate       |
|  SIGPIPE           |	terminate       |
|  SIGALRM           |	terminate       |
|  SIGTERM           |	terminate       |
|  SIGCHLD           |	ignore          |
|  SIGCONT           |	ignore(*)       |
|  SIGSTOP           |	stop(*)(+)      |
|  SIGTSTP           |	stop(*)         |
|  SIGTTIN           |	stop(*)         |
|  SIGTTOU           |	stop(*)         |   
|  SIGURG            |	ignore          |
|  SIGXCPU           |	coredump        |
|  SIGXFSZ           |	coredump        |
|  SIGVTALRM         |	terminate       |
|  SIGPROF           |	terminate       |
|  SIGPOLL/SIGIO     |	terminate       |
|  SIGSYS/SIGUNUSED  |	coredump        |
|  SIGSTKFLT         |	terminate       |
|  SIGWINCH          |	ignore          |
|  SIGPWR            |	terminate       |
|  SIGRTMIN-SIGRTMAX |	terminate       |
+--------------------+------------------+
|  non-POSIX signal  |  default action  |
+--------------------+------------------+
|  SIGEMT            |  coredump        |
+--------------------+------------------+

ignore - Nothing Happens
terminate - kill the process, i.e. all threads in the group, similar to exit_group.  The group leader (only) report WIFSIGNALED status to its parent.
coredump - write a core dump file describing all threads using the same Memory Management and then kill all those threads
stop - stop all the threads in the group, i.e. TASK_STOPPED state

Note that SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

Example how to catch a signal.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

static void signal_handler (int singnu)
{
	printf("Caught SIGTERM\n");
	exit (EXIT_SUCCESS);
}

int main (int argc, char *argv[]) 
{
	if (signal (SIGTERM, signal_handler) == SIG_ERR) {
		printf("Cannot handle signal\n");
		exit(EXIT_FAILURE);
	}
	
	for (;;)
		pause();
	
	return 0;
}

sources

http://www.tldp.org/LDP/tlk/kernel/processes.html
http://elixir.free-electrons.com/linux/v4.15-rc2/source/include/linux/signal.h