linux-sysprog · intermediate · ~15 min
Use fork + kill + waitpid to coordinate signal delivery.
Coordinate a parent and a child process through a signal: the parent forks a worker, then sends it SIGUSR1 to make it exit, and collects its exit status.
Implement int signal_child_to_exit(int seconds) that forks a child, signals it, and returns the child's exit code.
seconds: how long the parent waits before signalling the child. Use usleep for sub-second delays (e.g. when seconds == 0, give the child a brief moment to install its handler first).The function should:
fork() a child._exit(7), then pause().seconds, then kill(child, SIGUSR1).waitpid for the child and return its exit code (the value passed to _exit, i.e. 7).Returns -1 on any fork/wait failure.
signal_child_to_exit(0) -> 7 (child exits via its SIGUSR1 handler)
fork or waitpid failure returns -1.The classic IPC primitive: parent forks a worker, signals it to stop when work is done.
seconds: how long the parent waits before sending SIGUSR1 (use usleep for sub-second).
The child's exit code (7, from its _exit(7) handler), or -1 on fork/wait failure.
Child installs a SIGUSR1 handler that _exit(7)s, then pauses; parent kills then waitpids it.
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
int signal_child_to_exit(int seconds) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.