lesson depth
Mastery
not started · 0%

Linux Signals & Signal Handlers

Asynchronous signal delivery, signal masks, reentrant handlers, sigaction(), and graceful process termination.

Freshness: current15 min readComputer Science and Programming

Key Learning Outcomes

  • Implement safe signal handlers for graceful process shutdown
  • Manage process signal masks and signalfd file descriptors

Mental model

Linux Signals are asynchronous notifications delivered by the kernel to a process to inform it of an event (e.g. user interruption SIGINT, termination request SIGTERM, segmentation fault SIGSEGV).

Kernel receives signal event (e.g. SIGTERM)
Kernel updates Process Signal Pending Mask
Process transitions from Kernel to User Mode
Kernel interrupts execution & runs sigaction handler
Process cleans up & gracefully exits
Conceptual teaching model synthesized from:Linux Kernel Kernel.org Official Architecture & Systems Documentation

Theory

  • SIGTERM (15): Graceful termination request. Can be caught, handled, or ignored by application signal handlers to close sockets and flush transactions.
  • SIGKILL (9): Immediate forced process destruction by kernel. CANNOT be caught or ignored.
  • Async-Signal-Safe Functions: Functions that can be safely invoked inside a signal handler (e.g. write(), _exit()). Non-reentrant functions like printf() or malloc() MUST NOT be called in signal handlers.
c(25 lines)
1// C example: Safe Signal Handling using sigaction and atomic flags
2#include <stdio.h>
3#include <signal.h>
4#include <unistd.h>
5
6volatile sig_atomic_t keep_running = 1;
7
8void handle_sigterm(int sig) {
9 // Only set atomic flag inside signal handler
10 keep_running = 0;
11}
12
13int main() {
14 struct sigaction sa = {0};
15 sa.sa_handler = handle_sigterm;
16 sigaction(SIGTERM, &sa, NULL);
17
18 printf("Daemon running... Send SIGTERM to stop.\n");
19 while (keep_running) {
20 sleep(1);
21 }
22 printf("Graceful shutdown complete.\n");
23 return 0;
24}

Alternatives and trade-offs

  • Classic Signal Handlers (sigaction): Interrupts execution flow asynchronously; restricted to calling async-signal-safe functions.
  • signalfd() File Descriptors: Converts signal notifications into standard file descriptor read events; allows processing signals synchronously in epoll event loops.

Failure modes and misconceptions

  1. Deadlocks from Invoking printf or malloc in Handlers: Invoking printf() or malloc() inside a signal handler while the main thread holds an internal heap lock causes permanent process deadlocks.
  2. Ignoring SIGTERM in Container PID 1: Container processes running as PID 1 that fail to register SIGTERM handlers cause container runtimes to wait 10 seconds before forcibly destroying the container with SIGKILL.
Reflect before revealing the guide

Decision scenario

Register explicit SIGTERM signal handlers using sigaction or signalfd in all containerized applications to enable graceful socket closure and transaction flushing during rollouts.

Learning outcomes

  • Structure safe asynchronous signal handling using sigaction.
  • Distinguish graceful SIGTERM termination requests from uncatchable SIGKILL.
  • Eliminate handler deadlocks by avoiding non-reentrant functions like malloc() or printf().

Trade-offs

Signals provide essential process lifecycle control, but require strict adherence to async-signal-safe programming constraints inside handler functions.

Prerequisites & Related Concepts (2)

Private notes

0 words
Next