linux-sysprog · intermediate · ~15 min

Raise a signal to yourself

Deliver a signal synchronously with raise().

Challenge

raise() sends a signal to the current process; with a handler installed, the handler runs synchronously before raise() returns. Prove that round trip.

Task

Implement int raise_and_flag(void) that installs a SIGUSR1 handler which sets a flag, calls raise(SIGUSR1), and returns the flag.

Input

None.

Output

1 — the flag set by the handler after the signal is delivered.

Example

raise_and_flag()   ->   1

Rules

  • Install the handler before calling raise().
  • Use a static volatile sig_atomic_t flag for the handler.

Input format

None.

Output format

1 once the SIGUSR1 handler has run.

Constraints

Install the handler before raise(); use a sig_atomic_t flag.

Starter code

#include <signal.h>

int raise_and_flag(void) {
    /* TODO: install handler, raise(SIGUSR1), return the flag */
    return 0;
}

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