linux-sysprog · intermediate · ~15 min
Use kill() with your own PID.
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.
Implement int kill_self(void) that installs a SIGUSR1 handler which sets a flag, then calls kill(getpid(), SIGUSR1), and returns the flag.
None.
1 — the flag set by the handler after the self-sent signal is delivered.
kill_self() -> 1
static volatile sig_atomic_t flag for the handler.None.
1 once the SIGUSR1 handler has run.
Install the handler before kill(); use a sig_atomic_t flag.
#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.