Master the fundamental concepts of process management through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksWhen a child calls exit(), it becomes a zombie until the parent collects its status with wait() or waitpid(). The kernel keeps a minimal PCB skeleton so the parent can read the exit code. Ignore reaping and PIDs leak until the process table fills.
Both syscalls block until a child changes state:
WNOHANG pollsStatus encoding packs signal and exit code. On Linux, WIFEXITED(status) and WEXITSTATUS(status) decode the bits. For example, child exiting with code 3 leaves a status word whose low byte is 3 when exited normally.
Forgetting to reap zombies is a real production failure mode: Docker containers running a shell script as PID 1 famously accumulate zombies until the process table fills, which is why tini and dumb-init exist solely to call wait() on behalf of orphaned children. The WIFEXITED/WEXITSTATUS bit-packing you implement here is exactly the encoding bash's $? and git's exit-code checks rely on.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will implement blocking wait loops, zombie marking on exit, and status propagation to the parent. This task asks you to handle multiple children and show why shell pipelines depend on implicit reaping of background jobs.
Implement the kernel side of wait() / waitpid(): zombies, the encoded status word, the WNOHANG, WUNTRACED and WCONTINUED options, and orphan adoption. Pid 1 (init) exists from the start. It adopts orphans, and it reaps any child of its own the moment that child dies.
cLoading…
| Event | Status |
|---|---|
| exit(code) | (code & 0xff) << 8 |
| killed by signal | signo, plus 0x80 (core dumped) for SIGQUIT, SIGABRT and SIGSEGV |
| stopped | (signo << 8) | 0x7f |
| continued | 0xffff |
Signal numbers: SIGINT 2, SIGQUIT 3, SIGABRT 6, SIGKILL 9, SIGSEGV 11, SIGTERM 15, SIGCONT 18, SIGSTOP 19, SIGTSTP 20.
pid N reparented to 1), and a zombie among them is reaped at once ( init reaped pid N). If the dying child's own parent is 1, it is reaped immediately too.WUNTRACED, and a continue only to a waiter using WCONTINUED.-1 ECHILD. If they exist but have nothing to report, the result is 0 with WNOHANG, and otherwise blocks (nothing changes).cLoading…
waitpid echoes OPTS as typed, or 0 if there are none. ps lists every process not yet reaped, with state running, stopped or zombie. Errors: spawn from P: failed, exit P: no such live process, kill P: no such live process, kill P: unknown signal S, pid P already stopped, pid P not stopped, SIGCONT ignored. Init cannot exit or be killed.
Input:
cLoading…
Output:
cLoading…
WIFEXITED/WIFSIGNALED/WIFSTOPPED/WIFCONTINUED logic: (st & 0x7f) == 0, st == 0xffff, (st & 0xff) == 0x7f, and otherwise signalled.ps shows them.Hidden tests cover exit codes above 255 (only the low byte survives), SIGTERM and SIGKILL (no core), SIGQUIT and SIGABRT (core), SIGTSTP, stop and continue events reported once, waiting for a specific pid that is not a child, grandchildren adopted by init, init auto-reaping, and invalid commands.