UNIX SIGNALS

A handler runs in the middle of whatever you were doing

L315 MINOPERATING SYSTEMS
PREDICT

A SIGTERM handler calls printf, frees its buffers and calls exit(0). It has worked in every test. In production it sometimes hangs. Where was main when the hanging SIGTERM arrived?

A signal interrupts your process between two instructions, wherever it is. The kernel pushes a frame and runs your handler on the same thread. Main does not continue until the handler returns.

That makes the handler dangerous. If main was inside malloc holding the allocator’s lock, and the handler calls printf, malloc or free, the handler waits for a lock that only main can release. Main is frozen under the handler, so the process hangs. Only async-signal-safe functions, such as write and _exit, are allowed in a handler. The usual pattern: set a volatile sig_atomic_t flag, return, and let the main loop do the cleanup.

▶ SIMULATION
MAIN THREAD
state: running
loop iteration 1
now: top of loop
PROCESS STATE
arena lock: free
shutdown_flag: 0
cleanup done: no
SIGTERM HANDLER
installed: default
called: —
unsafe calls: 0
eventprocess running
✓ the handler calls only async-signal-safe functions✓ main never deadlocks
SCHEDULE
01[SETUP]install handler: printf, cleanup with free, exit(0)
02[MAIN]#1 malloc: lock arena
03[MAIN]#1 malloc: unlink free chunk
04[MAIN]#1 malloc: unlock, return
05[MAIN]#1 handle request
06[MAIN]#1 check shutdown_flag
07[MAIN]#2 malloc: lock arena
08[MAIN]#2 malloc: unlink free chunk
09[MAIN]#2 malloc: unlock, return
10[MAIN]#2 handle request
11[MAIN]#2 check shutdown_flag
12[KERNEL]report
Start of unsafe, no signal scenario. 12 steps.
void on_term(int sig) {
    printf("shutting down\n");   /* stdio lock, may malloc */
    free_all_buffers();           /* free(): arena lock     */
    exit(0);                      /* runs atexit, flushes   */
}
...
signal(SIGTERM, on_term);
while (1) {
    char *buf = malloc(4096);     /* SIGTERM here + handler = deadlock */
    handle_request(buf);
    free(buf);
}

Watch both invariants. UNSAFE, NO SIGNAL holds both because no signal arrives. UNSAFE, LUCKY exits 0 but still breaks the first invariant: the strip counts every unsafe call the handler made. UNSAFE, IN MALLOC hangs with the arena lock held. FIX: FLAG takes the same signal at the same point, makes 0 unsafe calls, and exits 0 from the main loop.

BREAK IT NOT YET

This server passes every test, because no test ever sends it SIGTERM. Inject one SIGTERM at a point that hangs the process forever.

GOAL: main deadlocks

FIX IT NOT YET

Return \{ handler, iteration \}. The grader calls iteration() in a loop and delivers SIGTERM (calls handler()) at every point inside it, including while api.malloc and api.free hold the arena lock. The handler may only use async-signal-safe calls (api.write, api._exit) or set a flag. After the signal, the loop must call api.cleanup() once and api.exit(0) by the next iteration. With no signal it must keep running.

Mental model:

a signal handler is code that can run between any two instructions of your program, including inside the C library. Treat it like an interrupt: touch nothing that might be half-updated or locked, set a flag (or write one byte to a pipe), and return. Do the real work in normal context, where locks and allocation are safe again.

IN PRODUCTION

Graceful shutdown on SIGTERM in containers and systemd units, SIGHUP config reloads, SIGCHLD reaping, and logging from a crash handler. Runtimes hide this for you: Go's signal.Notify delivers signals on a channel, and Python runs handlers between bytecodes on the main thread.

IN INTERVIEWS

“What can you do inside a signal handler?” Explain async-signal-safety with the malloc-lock deadlock, give the volatile sig_atomic_t flag pattern, mention the self-pipe trick or signalfd for a main loop blocked in poll, and say that SIGKILL cannot be caught.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.