Signals & default actions
Send signals and predict each one's default action.
By Invicly Tech, , in Linux System Programming. About 11 minutes to read.
In short
A signal is a small asynchronous notification sent to a process by the kernel, another process or the terminal. Each signal has a default action, such as terminating, dumping core, stopping or being ignored. Programs can catch most signals, but SIGKILL and SIGSTOP always take effect.
Explained simply
A signal is a tiny message to a process that says only one thing, such as "stop", "please quit" or "your child finished". It has no text, just a number with a name like SIGTERM or SIGINT. Think of it as a doorbell: the process hears that someone rang, and which bell it was, but nothing more.
Signals come from three places. The kernel sends them when something happens, for example SIGSEGV when a program touches memory it does not own. The terminal sends them when you press keys such as Ctrl-C. And other processes send them with the kill command or the kill() function.
Each signal has a default action: end the process, end it and save a memory snapshot (a core dump), pause it, continue it, or do nothing. A program can replace most defaults with its own code, for example to save work before quitting.
Two signals can never be caught: SIGKILL and SIGSTOP. They are the emergency brake. Whatever a program does, the system can always stop it or remove it. That is also why SIGKILL should be your last resort: the program gets no chance to clean up.
Press Play on a diagram to walk through it one step at a time.
- SIGTERM: polite shutdown request
- SIGINT: Ctrl-C
- SIGHUP: terminal closed
- SIGPIPE: write to a closed pipe
- SIGSEGV: invalid memory access
- SIGABRT: abort() called
- SIGFPE: arithmetic error
- SIGQUIT: Ctrl-backslash
- SIGSTOP and SIGTSTP stop
- SIGCONT resumes
- SIGCHLD is ignored by default
- SIGKILL and SIGSTOP cannot be caught
- CPU faults (kernel)
- SIGSEGV: bad memory access
- SIGFPE: divide by zero
- SIGBUS: bad mapped file access
- SIGILL: illegal instruction
- Kernel events
- SIGCHLD: child changed state
- SIGPIPE: write to a closed pipe
- SIGALRM: alarm timer fired
- SIGKILL: out-of-memory killer
- Terminal
- SIGINT: Ctrl-C
- SIGTSTP: Ctrl-Z
- SIGHUP: terminal closed
- SIGWINCH: window resized
- Other processes
- kill, pkill, killall
- systemd and docker stop: SIGTERM
- Self: raise() and abort()
In depth
Signal numbers you will meet
On x86-64 and ARM Linux the common numbers are: SIGHUP 1, SIGINT 2, SIGQUIT 3, SIGILL 4, SIGTRAP 5, SIGABRT 6, SIGBUS 7, SIGFPE 8, SIGKILL 9, SIGUSR1 10, SIGSEGV 11, SIGUSR2 12, SIGPIPE 13, SIGALRM 14, SIGTERM 15, SIGCHLD 17, SIGCONT 18, SIGSTOP 19, SIGTSTP 20, SIGTTIN 21 and SIGTTOU 22. Some other architectures, such as MIPS and SPARC, use different numbers, so always write names in code and scripts.
Above the standard signals are the real-time signals, from SIGRTMIN to SIGRTMAX (usually 34 to 64 with glibc, which keeps two for its own thread library). Unlike standard signals, real-time signals are queued: if you send five, five are delivered, in order, and each can carry a small value with sigqueue. SIGUSR1 and SIGUSR2 have no fixed meaning; programs use them for things like reopening log files.
- kill -l lists every name and number
- kill -l 15 prints TERM
- kill -s TERM PID and kill -TERM PID are the same
Generated, pending, delivered
A signal goes through stages. First it is generated: by the kernel, the terminal or kill(). It is then pending, recorded as one bit in a pending set. Each thread has its own pending set, and the process has a shared one. If the signal is blocked by the signal mask, it simply stays pending.
Delivery happens when the target is about to return from the kernel to user code, for example after a system call or a timer interrupt, and the signal is not blocked. The kernel then takes the action: default, ignore, or run the handler. Because a standard signal is just one bit, sending SIGCHLD three times while it is pending still delivers it once. Programs must never count signals to count events.
- 1Generated
Kernel, terminal or kill() raises it.
- 2Pending
One bit set; repeats merge.
- 3Blocked?
If masked, it waits here.
- 4Delivered
On the way back to user mode.
- 5Action
Default, ignore or run the handler.
Sending signals: kill, raise and who may send
kill(pid, sig) has four forms. A positive pid targets one process, 0 targets every process in your own process group, -1 targets every process you are allowed to signal, and a value below -1 targets the process group with that number. Signal number 0 sends nothing but still runs the checks, so kill(pid, 0) tells you whether a process exists and whether you may signal it.
An unprivileged sender's real or effective UID must match the target's real or saved set-user-ID. Root, or a process with the CAP_KILL capability, may signal anyone. raise(sig) sends a signal to the calling thread itself, and abort() raises SIGABRT. At the command line, pkill and killall pick processes by name; pkill -f matches the full command line, so check with pgrep -f first.
- kill(pid, 0) fails with ESRCH if no such process, EPERM if not yours
- pgrep -a nginx previews what pkill nginx would hit
- Zombies still exist for kill(pid, 0) until reaped
Exit codes, core dumps and stop timeouts
When a signal ends a process, the shell reports the exit code 128 plus the signal number. So 130 means SIGINT (Ctrl-C), 137 means SIGKILL (very often the out-of-memory killer), 139 means SIGSEGV and 143 means SIGTERM. Seeing 137 in a container log is a strong hint to check memory limits and dmesg.
The core action writes a core dump only if the core size limit allows it (ulimit -c, often 0 by default) and according to kernel.core_pattern. On systemd machines that pattern usually pipes cores to systemd-coredump, and coredumpctl list shows them.
Polite shutdown is always SIGTERM first. docker stop sends SIGTERM and waits 10 seconds before SIGKILL. systemd sends SIGTERM and waits TimeoutStopSec, 90 seconds by default. Kubernetes waits terminationGracePeriodSeconds, 30 by default.
Why SIGKILL and SIGSTOP are special, and their limits
The kernel refuses to let anyone catch, block or ignore SIGKILL and SIGSTOP; sigaction returns EINVAL if you try. This guarantees an administrator can always stop or remove a misbehaving program. SIGSTOP freezes a process exactly where it is, and SIGCONT resumes it; debuggers and job control rely on this.
There are still cases where SIGKILL seems not to work. A task in uninterruptible sleep (state D) dies only when its kernel wait finishes, unless that wait is of the killable kind. A zombie is already dead, so the signal changes nothing. And the init process of a PID namespace, such as PID 1 in a container, does not receive SIGKILL from processes inside its own namespace; only a sender in a parent namespace can kill it that way.
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1List common signals and their numbers~40m
- 2Send signals with kill and raise~40m
- 3Explain terminate, core, stop and ignore~40m
- 4Explain why SIGKILL cannot be caught~40m
Key points
- kill sends a signal to a PID; kill -l lists all signals.
- SIGTERM lets a program clean up; use SIGKILL only as a last resort.
- Standard signals are not queued: two identical pending signals merge into one.
- A signal can arrive between any two instructions of your program.
Key terms
Tap a card to flip it. 0/6 known
Examples
kill -l
sleep 100 &
kill -TERM $!
trap 'echo got SIGINT' INT # shell-level handler
kill -INT $$
kill -9 1234 # SIGKILL, cannot be caughtSend signals and catch one in the shell.
A safe stop script: ask first, force later
You want to restart a worker without losing its in-flight job. Step 1: send SIGTERM so it can finish and clean up. Step 2: poll with kill -0 once per second, which checks existence without sending anything. Step 3: only if it is still alive after 20 seconds, send SIGKILL. Step 4: report which path was taken. This is the same pattern systemd, Docker and Kubernetes follow.
pid=$(pgrep -o -f myworker) || { echo "not running"; exit 0; }
kill -TERM "$pid"
for i in $(seq 1 20); do
kill -0 "$pid" 2>/dev/null || { echo "stopped cleanly after ${i}s"; exit 0; }
sleep 1
done
echo "still alive, forcing"
kill -KILL "$pid"Is that process alive, and is it mine?
kill() with signal 0 is the standard way to test a PID. If it succeeds, the process exists and you may signal it. If it fails, errno tells you why: EPERM means it exists but belongs to someone else, ESRCH means there is no such process. Try it on your shell's PID, on 1 (EPERM unless you are root) and on a large unused number.
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) { fprintf(stderr, "usage: %s pid\n", argv[0]); return 2; }
pid_t pid = (pid_t)strtol(argv[1], NULL, 10);
if (pid <= 0) { fprintf(stderr, "pid must be positive\n"); return 2; }
if (kill(pid, 0) == 0) puts("alive, and we may signal it");
else if (errno == EPERM) puts("alive, but we may not signal it");
else if (errno == ESRCH) puts("no such process");
else perror("kill");
return 0;
}Quick reference
- Always use names (SIGTERM), not numbers, because numbers differ between CPU architectures
- Exit code 128+N means killed by signal N: 130 INT, 137 KILL, 139 SEGV, 143 TERM
- kill -0 PID tests existence and permission without sending a signal
- kill -- -PGID signals a whole process group
- Standard signals merge while pending; real-time signals queue
- ulimit -c unlimited plus coredumpctl list helps you find core dumps
- Stop order: SIGTERM, wait, then SIGKILL only if needed
- sigaction on SIGKILL or SIGSTOP fails with EINVAL
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Start sleep 1000 in the background, stop it with SIGSTOP, confirm its state, resume it with SIGCONT, then end it with SIGTERM and check the exit code.
- Exercise 2medium
Write a shell script that traps SIGTERM and SIGINT, prints which one arrived, removes a temp file and exits with the usual 128+N code.
- Exercise 3hard
Write a C program that crashes on purpose with SIGSEGV, run it under a parent you write, and have the parent print the signal number and whether a core was dumped.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between SIGTERM and SIGKILL?
- SIGTERM asks the program to exit and can be caught, so it may save data and clean up. SIGKILL ends it immediately and cannot be caught, blocked or ignored.
- What does kill -9 actually mean?
- It sends signal number 9, which is SIGKILL on Linux. Writing kill -KILL is clearer and portable.
- What is SIGHUP used for in daemons?
- Its original meaning is that the terminal hung up, but many daemons such as nginx catch it as a request to reload their configuration.
- Why did my program get SIGPIPE?
- It wrote to a pipe or socket whose reading side was closed. Ignore SIGPIPE and check write for EPIPE, or use MSG_NOSIGNAL on sockets.