linux-sysprog · intermediate · ~15 min
Catch SIGINT to shut down gracefully.
Installing a SIGINT handler turns Ctrl-C from instant termination into a flag your program can check and shut down cleanly. Demonstrate the catch.
Implement int catch_sigint(void) that installs a SIGINT handler (which sets a flag), raises SIGINT, and returns the flag.
None.
1 — the flag set by the handler, proving SIGINT was caught rather than fatal.
catch_sigint() -> 1
static volatile sig_atomic_t flag.None.
1 once the SIGINT handler has run.
Install the handler before raising; use a sig_atomic_t flag.
#include <signal.h>
int catch_sigint(void) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.