Linux System Programming · intermediate · ~10 min
Send signals between processes with kill() — safely, locally, and only to processes you own.
kill() really doeskill(pid, signo) sends a signal to another process. Despite its name, it is not only about terminating processes. It is a general-purpose way to signal them.
Common uses:
SIGUSR1 — tell a process to do something custom, such as reload its configuration.SIGTERM — politely ask a process to exit.0 — send no signal at all, just probe whether the pid still exists. If the process is gone, kill() fails with ESRCH.On Linux, you may signal a process if either of these is true:
uid = user ID, the number the system uses to identify the account a process belongs to.
The practical result: a normal user can only signal their own processes, never another user's.
This lesson is about signalling cooperating processes:
Do not go hunting for arbitrary pids on a shared machine.
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(void) {
pid_t child = fork();
if (child == 0) {
/* child: wait for a signal */
pause();
printf("child got woken up\n");
return 0;
}
sleep(1);
kill(child, SIGUSR1); /* SIGUSR1 default = terminate */
/* In a real program, the child would handle SIGUSR1 cleanly. */
return 0;
}
kill(pid, 0). It does not send a signal. It only checks whether the signal could be delivered. This is handy for asking "is this pid still alive?", but it is easy to misread as actually sending something.kill(pid, signo) sends a signal to another process; it is general-purpose signalling, not just termination.0 to probe whether a pid still exists (you get ESRCH if it is gone).