linux-sysprog · intermediate · ~15 min

Catch Ctrl-C

Catch SIGINT to shut down gracefully.

Challenge

Implement int catch_sigint(void) that installs a SIGINT handler (setting a flag), raises SIGINT, and returns the flag (1) — so Ctrl-C is handled instead of terminating the process.

Starter code

#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.