Knowledge

Linux: signals

Subject: .../LINUX

A signal is a short notification

A process is a running program. Sometimes the operating system or another program needs to notify it that something happened. A signal is a small notification of that kind: it can report a child process changing state, an invalid memory access, or a request to stop. Pressing Ctrl+C in a terminal commonly sends SIGINT to the foreground program. A signal is not a channel for a long message; real-time signals can also carry a small value. The program chooses how to respond.

A C program can set a flag in a signal handler, a function that runs when a signal arrives. sig_atomic_t is an integer type intended for simple values that a handler may read or write safely. For example:

#include <signal.h>

static volatile sig_atomic_t stop_requested;

static void handle_signal(int signo)
{
    (void) signo;
    stop_requested = 1;
}

This small handler records the request. The main part of the program can check stop_requested during normal execution and perform cleanup there. The code shows only the handler body; a complete program must install it with an interface such as sigaction() and check for errors.

Choose what happens on delivery

A signal has a disposition, the response chosen for it. A program can usually use the default action, ignore the signal, or install a handler. Default actions include terminating, stopping, continuing, or terminating and writing a core dump. SIGTERM requests termination; SIGCHLD reports that a child process changed state.

SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. A program therefore cannot prevent their actions by installing a handler, and it cannot run cleanup code for either one. Other signals can be blocked temporarily. A blocked signal may remain pending until it is unblocked, after which the kernel can deliver it according to its disposition.

Standard signals can coalesce: if the same signal is generated several times while blocked, the program may receive one pending instance rather than a separate notification for each event. Linux real-time signals have queued delivery and ordering rules, but applications still need to handle queue limits and platform-specific behavior.

Keep handlers safe and small

A signal can interrupt a program while a library function is changing internal data. Calling an arbitrary library function from a handler can corrupt that data or deadlock. C and POSIX allow only a defined set of async-signal-safe functions in such a handler. A simple flag lets the main code defer complex work until it resumes normally.

In a multithreaded program, signal masks control which threads can receive a signal. Process-directed and thread-directed signals have different delivery rules; a shared flag alone may not be enough. Signals are best for brief notifications. Use a pipe, socket, or message queue to send structured data between processes, and synchronization primitives to coordinate shared state. sigwait() lets a program wait for a signal synchronously, keeping its logic out of an asynchronous handler.

Run kill -l or read the local signal(7) manual to check signal names and default actions. Numeric signal values can differ among Unix-like systems, so portable source should use symbolic names. When debugging, distinguish a signal being generated, pending, blocked, delivered, and handled; those are separate stages.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful