Separate termination from collection
A child can finish executing while the system still retains its process identity and exit information for the parent. This terminated-but-unreaped state is called a zombie. It does not mean the child continues running its application workload or consuming CPU in a loop. The parent must collect the completion status and perform the intended bookkeeping. Create only one child owned by this experiment; do not signal existing processes or change host process-management settings. The procedure requires Python on Linux with fork and waitid support. It is not a portable Windows example, and an environment that prohibits child creation should report the experiment as unavailable rather than substituting fabricated output.
Use state transitions rather than a guessed sleep
The child exits with status seven. The parent calls waitid with WNOWAIT so it can observe termination without consuming the child’s waitable status. It then calls waitpid to reap the same child and checks the decoded exit code. This ordering relies on a reported process state, not a sleep chosen in the hope that the child has already finished. Give the script an outer execution timeout in the experiment runner. A special SIGCHLD disposition or another child-reaping mechanism can change the behavior, so keep the parent process simple and document its assumptions. The example is intentionally independent of a service manager that might otherwise own lifecycle handling.
import os
pid = os.fork()
if pid == 0:
os._exit(7)
try:
observed = os.waitid(os.P_PID, pid, os.WEXITED | os.WNOWAIT)
assert observed.si_pid == pid and observed.si_status == 7
got, status = os.waitpid(pid, 0)
assert got == pid and os.waitstatus_to_exitcode(status) == 7
try:
os.waitpid(pid, os.WNOHANG)
except ChildProcessError:
print(pid, 'exit observed, then reaped exactly once')
else:
raise AssertionError('unexpected waitable child')
finally:
try:
os.waitpid(pid, 0)
except ChildProcessError:
passCheck the single collection of exit status
After waitpid reaps the child, another waitpid for that child normally reports that no waitable child exists; Python exposes this as ChildProcessError. This is not proof that no process with that integer PID can ever exist on the host. PIDs can be reused, and the parent-child relationship controls which processes are waitable. The verified property is narrower: this parent collected its own child’s exit status once and cannot collect that same status again. Saving a PID in a variable does not preserve process ownership indefinitely. Keep process identity, relationship and lifecycle state separate when designing monitoring or asynchronous process-control code.
Design the reaping path before scaling child creation
One zombie does not immediately consume all memory, but continually creating unreaped children can exhaust management resources or process identifiers. A long-running parent may combine nonblocking waits with child-state notifications. Do not assume that one notification always corresponds to exactly one terminated child; the reaping logic must handle all available child states according to its contract. Review creation failure, exceptions in the parent and shutdown as well as normal completion. Discarding exit-status handling merely to hide visible zombies can also discard information needed to detect failed work. Identify which component owns the wait operation and how completion is reported to the application before increasing process concurrency.
Keep the experiment’s scope explicit
This single parent-child pair does not test reparenting after parent exit, container PID namespaces, subreapers or service-manager behavior. A stopped child also differs from a terminated child, and waitid flags determine which changes are reported. Select WEXITED here so the example concerns normal termination. Record the child PID, initial termination information and decoded status after reaping. A process name in a listing is not enough to establish those state transitions. On an exceptional path, clean up and reap only the child owned by the experiment. Do not expand the procedure into removing or manipulating unrelated processes on a shared machine.