linux-sysprog · intermediate · ~15 min

Parent process signals a child

Use fork + kill + waitpid to coordinate signal delivery.

Challenge

Implement int signal_child_to_exit(int seconds) that:

  1. fork()s a child process.
  2. In the child, installs a SIGUSR1 handler that calls _exit(7). Then pauses.
  3. In the parent, sleeps seconds (use usleep for sub-second), then kill(child, SIGUSR1).
  4. Parent waits for the child and returns the child's exit status (the value passed to _exit).

Return -1 on any fork/wait failure.

Why this matters

The classic IPC primitive: parent forks a worker, signals it to stop when work is done.

Starter code

#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
int signal_child_to_exit(int seconds) {
    /* TODO */
    return -1;
}

Background lessons

Up next

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