linux-sysprog · intermediate · ~15 min

Signal yourself with kill

Use kill() with your own PID.

Challenge

kill() isn't only for killing — it delivers any signal to a target PID, including your own. Send yourself SIGUSR1 and confirm your handler ran.

Task

Implement int kill_self(void) that installs a SIGUSR1 handler which sets a flag, then calls kill(getpid(), SIGUSR1), and returns the flag.

Input

None.

Output

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

Example

kill_self()   ->   1

Rules

  • Install the handler before sending the signal.
  • 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 kill(); use a sig_atomic_t flag.

Starter code

#include <signal.h>
#include <unistd.h>

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

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