linux-sysprog · beginner · ~10 min
The dispatch-table pattern for signal handling.
You're inside a server's signal-handling loop. The signals arrive
via signalfd. Implement int classify_signal(int sig) that returns:
0 for SIGINT (2) and SIGTERM (15) — clean shutdown1 for SIGHUP (1) — config reload2 for SIGCHLD (17) — child reaper-1 for any other signalUse the literal signal numbers above (so the test fixture doesn't need to
include <signal.h>).
signalfd turns signals into normal file descriptors — meaning your dispatch can be a switch in normal code, not an async-signal-safe handler. Demonstrate the switch.
Signal number.
0/1/2/-1.
No <signal.h>; the numbers are baked into the spec.
int classify_signal(int sig) { /* TODO */ return -1; }
Returning 1 for SIGINT (mistakenly thinking it's a reload).
Unknown signal (-1). Signal 0 (-1).
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.