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;
}

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.