linux-sysprog · intermediate · ~15 min

Spawn child and collect exit code

`fork`, `_exit`, `waitpid`; the parent/child split.

Challenge

Implement int spawn_exit(int code) that:

  1. forks,
  2. in the child, calls _exit(code),
  3. in the parent, waitpid()s for the child and returns the child's exit status (the value extracted by WEXITSTATUS).

Starter code

#include <unistd.h>
#include <sys/wait.h>

int spawn_exit(int code) {
    /* TODO */
    return -1;
}

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