Skip to main content
Linux System Administration:

Introduction to Linux signals

Summary

Introduction to Linux signals, software interrupts used for inter-process communication and event handling. The article covers sending, receiving, and managing signals, including examples like using Ctrl+c for SIGINT and blocking signals with a signal mask.

Introduction #

Linux signals provide a mechanism for processes to communicate with each other (inter-process communication mechanism) and with the operating system. A signal is a software-generated interrupt.

Signals allow asynchronous notifications between processes or between the kernel and a process. A process can send a signal to another process, and the receiving process can decide how to handle it.

Signals are used for various purposes, such as terminating processes, handling errors, or performing custom actions in response to specific events.

Full list of Linux signals #

The following table lists all possible Linux signals:

Signal#Send SignalActionDescriptionTypePOSIX
SIGHUP1kill -SIGHUP <pid>TerminateHangup detectedSignalYes
SIGINT2Ctrl+C or kill -SIGINT <pid>TerminateInterrupt from keyboardInterruptYes
SIGQUIT3Ctrl+\ or kill -SIGQUIT <pid>Core DumpQuit from keyboardSignalYes
SIGILL4kill -SIGILL <pid>Core DumpIllegal instructionSignalYes
SIGTRAP5kill -SIGTRAP <pid>Core DumpTrace/breakpoint trapSignalYes
SIGABRT6abort() or kill -SIGABRT <pid>Core DumpAbnormal terminationSignalYes
SIGBUS7kill -SIGBUS <pid>Core DumpBus errorSignalYes
SIGFPE8kill -SIGFPE <pid>Core DumpFloating-point exceptionSignalYes
SIGKILL9kill -SIGKILL <pid>TerminateKill signalSignalYes
SIGUSR110kill -SIGUSR1 <pid>TerminateUser-defined signal 1SignalYes
SIGSEGV11kill -SIGSEGV <pid>Core DumpSegmentation faultSignalYes
SIGUSR212kill -SIGUSR2 <pid>TerminateUser-defined signal 2SignalYes
SIGPIPE13kill -SIGPIPE <pid>TerminateBroken pipeSignalYes
SIGALRM14kill -SIGALRM <pid>TerminateAlarm clockSignalYes
SIGTERM15kill -SIGTERM <pid>TerminateTermination signalSignalYes
SIGSTKFLT16kill -SIGSTKFLT <pid>TerminateStack faultSignalNo
SIGCHLD17kill -SIGCHLD <pid>IgnoreChild process terminatedSignalYes
SIGCONT18kill -SIGCONT <pid>ContinueContinue processSignalYes
SIGSTOP19kill -SIGSTOP <pid>StopStop processSignalYes
SIGTSTP20Ctrl+Z or kill -SIGTSTP <pid>StopTerminal stopSignalYes
SIGTTIN21kill -SIGTTIN <pid>StopBackground process reading from terminalSignalYes
SIGTTOU22kill -SIGTTOU <pid>StopBackground process writing to terminalSignalYes
SIGURG23kill -SIGURG <pid>IgnoreUrgent socket conditionSignalYes
SIGXCPU24kill -SIGXCPU <pid>Core DumpCPU time limit exceededSignalYes
SIGXFSZ25kill -SIGXFSZ <pid>Core DumpFile size limit exceededSignalYes
SIGVTALRM26kill -SIGVTALRM <pid>TerminateVirtual alarm clockSignalYes
SIGPROF27kill -SIGPROF <pid>TerminateProfiling timer expiredSignalYes
SIGWINCH28kill -SIGWINCH <pid>IgnoreWindow size changeSignalYes
SIGIO29kill -SIGIO <pid>TerminateI/O now possibleSignalYes
SIGPWR30kill -SIGPWR <pid>TerminatePower failureSignalNo
SIGSYS31kill -SIGSYS <pid>Core DumpBad system callSignalYes

What happens when a process receives a signal? #

When a process receives a signal, it can:

  1. Ignore it (if allowed).
  2. Catch and handle it using a signal handler.
  3. Perform the default action assigned to the signal.

Examples #

  • When you press Ctrl+C in a terminal, the shell sends a SIGINT signal to the foreground process. If the process does not handle it, it terminates.
  • A segmentation fault (SIGSEGV) typically results in the process being terminated with a core dump.

How a process sends a signal #

A process can send a signal using:

  1. The kill command: kill -SIGTERM <pid>.
  2. The kill() system call in a C program.

Examples #

  • Using kill: kill -SIGKILL 1234 terminates process with PID 1234.
  • Using raise() in C: raise(SIGTERM); sends SIGTERM to itself.

Signal mask #

A signal mask is a mechanism that allows a process to block certain signals from being delivered until it is ready to handle them. It is useful when a process wants to ensure atomic execution of critical sections without interruptions.

  • Signals can be blocked using sigprocmask() in C.
  • The sigpending() function can check for pending signals.
  • Once unblocked, pending signals are delivered to the process.

Example #

In a C program:

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

int main() {
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, SIGINT);
  sigprocmask(SIG_BLOCK, &mask, NULL);
  
  printf("SIGINT is now blocked. Press Ctrl+C, but it will not terminate.\n");
  sleep(10);
  
  sigprocmask(SIG_UNBLOCK, &mask, NULL);
  printf("SIGINT is now unblocked.\n");
  return 0;
}

This program blocks SIGINT temporarily and unblocks it after 10 seconds.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

Introduction to Linux signals by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/introduction-to-linux-signals">Introduction to Linux signals</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the Ditig legal page.

All Topics

Random Quote

“Learning from conventions will make your site better.”

Jeffrey Veen American designer and design strategistThe Art & Science of Web Design, - IT quotes