The problem
A user runs a long command, presses Ctrl-C, and expects the command to stop. Most of the time it does. Sometimes it does not, sometimes the shell keeps running, and sometimes a program prints a message and then keeps going. To explain those cases, you need to know exactly which component decides what Ctrl-C means. This case study traces the path one key press takes through the system, and then it looks at the three places where the result can differ.
Step one: the emulator sends a byte
The terminal emulator does not know what Ctrl-C means. It reads the key press and writes the byte 0x03 to the master side of the pseudo-terminal. The same byte is produced for every program, because the emulator is only a front end. The byte travels through the kernel to the slave side, which is the input that bash and the program read from.
Step two: the line discipline decides
The line discipline is kernel code between the two ends of the pseudo-terminal. It checks the byte against the control characters in the terminal settings. The stty -a command prints those settings, and the intr entry is the byte that means interrupt. In the lab that byte is ^C.
stty -a | grep -o "intr = [^;]*"When the line discipline sees the interrupt byte and the signal generation flag is on, it does not pass the byte to the reader. Instead it sends SIGINT to the foreground process group of the terminal. This decision is made in the kernel, which is why a program that is blocked, or that ignores its input, still receives the interrupt.
Step three: the kernel delivers the signal
SIGINT is delivered to every process in the foreground group. Each process has a signal disposition for SIGINT. The default action is to terminate, so a program that does nothing special exits at once, and the kernel records the signal as the reason for the exit. A process can instead install a handler, which runs when the signal arrives, or it can ignore SIGINT entirely.
The shell is a special case. Interactive bash ignores SIGINT while it waits for you, so that Ctrl-C at the prompt only cancels the current line. When bash starts a foreground job, it restores the default action for the child, so the child receives the interrupt and stops.
Step four: the status the shell reports
When the child is killed by SIGINT, the shell does not see an ordinary exit code. It reports 128 plus 2, which is 130, in the special parameter $?. A script can test for that value to tell an interrupted run apart from a normal failure.
sleep 300 &
kill -INT $!
wait $!; echo "status $?"The command sends SIGINT to a background sleep directly, which is the same signal Ctrl-C sends to the foreground group. The status printed is 130. The number follows the convention described in the signals lesson, and the value tells the caller that the job ended by a signal.
Where the outcome can change
The path has three places where the result can differ from what you expect. The first is the terminal state. A full-screen program such as vim switches the tty to raw mode, which turns off signal generation. Ctrl-C then arrives as an ordinary byte, and the program decides what to do. That is why Ctrl-C can cancel a pending command in vim without ending the editor.
The second is the program itself. A program can install a handler for SIGINT, which lets it finish a write, remove a temporary file, or ask the user to confirm. A program that catches SIGINT and keeps running does not stop when you press Ctrl-C a single time. Pressing it again usually works only if the program was written to handle a second press.
The third is the process group. The signal goes to the foreground group only. A background job started with an ampersand is in a different group, so Ctrl-C does not reach it. A pipeline is one group, so Ctrl-C stops every stage of the pipeline together.
A script started with bash in the foreground can ignore SIGINT with trap "" INT. Such a script continues to run after Ctrl-C, which surprises people who expect every script to stop when they press the key.
What to check when Ctrl-C does nothing
The steps above give a checklist. First, confirm the interrupt byte with stty -a. Then look at the program state with ps, because a process in state D cannot be interrupted until its I/O completes. Finally, check whether the program has a handler, which you can read from the SigCgt and SigIgn lines in /proc/PID/status. Each line is a bit mask of the signals with that disposition.
grep -E "Sig(Cgt|Ign)" /proc/$$/statusThe output shows the caught and ignored masks for your shell. Each one is a hexadecimal number in which each bit stands for one signal. A process that ignores or catches SIGINT has the matching bit set in one of those lines, which explains why it did not stop when you pressed the key.