linux-sysprog · intermediate · ~15 min

Count raised signals

Observe repeated synchronous delivery.

Challenge

Each synchronous raise() runs the handler exactly once, so raising a signal twice increments a counter to 2. Demonstrate that.

Task

Implement int raise_twice(void) that installs a SIGUSR1 handler which increments a counter, calls raise(SIGUSR1) twice, and returns the counter.

Input

None.

Output

2 — the counter after two synchronous deliveries.

Example

raise_twice()   ->   2

Rules

  • Use a static volatile sig_atomic_t counter.
  • Install the handler before raising.

Input format

None.

Output format

2 — the handler count after raising SIGUSR1 twice.

Constraints

Use a sig_atomic_t counter; install the handler first.

Starter code

#include <signal.h>

int raise_twice(void) {
    /* TODO */
    return 0;
}

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