Linux System Programming · intermediate · ~10 min

kill() — sending a signal to another process

Send signals between processes using kill() — safely, locally, only to processes you own.

Lesson

kill(pid, signo) sends a signal to another process. Despite the name, it's general-purpose signalling; you can send SIGUSR1 to tell a process to reload its config, SIGTERM to ask it to exit, or 0 to probe whether the pid is still alive (no signal sent, but you get ESRCH if the process is gone).

Permission rules (Linux): you can signal a process if you're root, or if your real or effective uid matches its real or saved uid. So a normal user can only kill their own processes — never another user's.

Ethical reminder: this lesson is about signalling cooperating processes (your own child, a daemon you wrote, a test fixture). Don't go looking for arbitrary pids on a shared machine.

Code examples

#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;
}

Common mistakes

  • Forgetting that kill(pid, 0) does NOT send a signal; it only checks deliverability. Useful for "is this pid still alive?" but easy to misread.
  • Race: the child pid can be reused by another process after the original exits. Always send signals to a pid you spawned and haven't reaped yet.

Summary

kill(pid, signo) sends a signal to another process. Send 0 to probe. You can only signal processes you own (same uid).

Practice with these exercises