Common signals: SIGINT, SIGTERM, SIGKILL
What you'll learn: How to distinguish between the three most frequent signals used to stop a running process.
In systems programming, signals are like "interrupting phone calls" sent to your program by the operating system or the user. While there are dozens of signals, you will encounter these three most often when trying to stop a process.
The Polite Request: SIGINT
SIGINT stands for "Signal Interrupt." This is what happens when you press Ctrl+C in your terminal.
Think of SIGINT as a colleague tapping you on the shoulder while you're typing. It is a polite request to stop. Because it is polite, your program can "catch" this signal, finish saving a file, close a database connection, and then exit gracefully.
The Firm Command: SIGTERM
SIGTERM stands for "Signal Terminate." This is the default signal sent by the kill command.
If SIGINT is a tap on the shoulder, SIGTERM is a manager telling you the office is closing in five minutes. It is the standard way to tell a program to shut down. Like SIGINT, the program can catch this signal to perform cleanup duties before it disappears.
The "Delete" Key: SIGKILL
SIGKILL is the nuclear option. It cannot be caught, blocked, or ignored.
When the OS sends SIGKILL (via kill -9), it doesn't even talk to your program. Instead, the kernel immediately snatches the process out of memory and deletes it. There is no cleanup, no saving files, and no "goodbye." Use this only as a last resort when a program is frozen and unresponsive to the other two.
How they look in code
While you can't catch SIGKILL, you can set up "listeners" for the others:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handle_stop(int sig) {
printf("\nCaught signal %d. Cleaning up files before I go!\n", sig);
// Logic to save work goes here
_exit(0);
}
int main() {
// Registering handlers for "polite" signals
signal(SIGINT, handle_stop);
signal(SIGTERM, handle_stop);
printf("I'm working... try pressing Ctrl+C\n");
while(1) {
sleep(1);
}
return 0;
}
Key takeaway
Use SIGINT and SIGTERM for graceful shutdowns, and reserve SIGKILL for emergencies when a process refuses to die.