Concept lesson

Linux Signals & Signal Handlers

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

lesson
Freshness: current15 min read
Mastery
not started · 0%

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 example: Safe Signal Handling using sigaction and atomic flags
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

volatile sig_atomic_t keep_running = 1;

void handle_sigterm(int sig) {
    // Only set atomic flag inside signal handler
    keep_running = 0;
}

int main() {
    struct sigaction sa = {0};
    sa.sa_handler = handle_sigterm;
    sigaction(SIGTERM, &sa, NULL);

    printf("Daemon running... Send SIGTERM to stop.\n");
    while (keep_running) {
        sleep(1);
    }
    printf("Graceful shutdown complete.\n");
    return 0;
}

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.

Evidence assessment

Theory and decision mastery

not-started · 0%
theory0%
decision0%
activityNot mapped
projectNot mapped
1. What is the core architectural principle governing linux signals signal handling?
2. What primary operational trade-off must be managed when configuring linux signals signal handling?
3. Which failure mode is most commonly observed when linux signals signal handling is misconfigured?

Decision scenario

You are designing a high-concurrency production system requiring reliable execution of linux signals signal handling under heavy traffic load.

Which architectural decision ensures maximum resilience, scalability, and system stability?

Primary sources