linux-sysprog · beginner · ~15 min

Force-quit on second Ctrl-C

Model the two-strike shutdown policy.

Challenge

Many programs treat the first Ctrl-C as "shut down cleanly" and a second as "force quit now". Model that two-strike policy.

Task

Implement int should_force_exit(int sigint_count) that returns 1 if sigint_count is 2 or more, else 0.

Input

  • sigint_count: how many times SIGINT (Ctrl-C) has been received.

Output

1 if sigint_count >= 2, else 0.

Example

should_force_exit(1)   ->   0   (first Ctrl-C: graceful)
should_force_exit(2)   ->   1   (second: force quit)
should_force_exit(5)   ->   1

Edge cases

  • Count of 0 or 1: return 0.

Input format

sigint_count: number of SIGINTs received.

Output format

1 if sigint_count >= 2, else 0.

Constraints

First Ctrl-C is graceful; the second forces exit.

Starter code

int should_force_exit(int sigint_count) {
    /* TODO */
    return 0;
}

Common mistakes

Calling non-reentrant functions (like printf or malloc) inside signal handlers; neglecting sig_atomic_t or volatile qualifier for shared flags.

Edge cases to handle

Multiple instances of the same signal queued together; signal arriving while handler is already active; interrupted system calls (EINTR).

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.