linux-sysprog · intermediate · ~15 min
`fork`, `_exit`, `waitpid`; the parent/child split.
Fork a child process, have it exit with a given code, and collect that code in the parent.
Implement int spawn_exit(int code) that:
fork()s,_exit(code),waitpid()s for the child and returns its exit status (the value WEXITSTATUS extracts).No main — the grader calls it.
One int argument code — the exit code the child should produce (e.g. 0..42).
The child's exit status as an int (equal to code on a normal exit), or -1 on a fork/wait error.
spawn_exit(0) -> 0
spawn_exit(7) -> 7
spawn_exit(42) -> 42
fork failure returns -1._exit in the child (not exit) to skip atexit handlers; waitpid in the parent, then WEXITSTATUS.One int argument code (the exit code the child should produce).
The child's exit status as an int (equals code on normal exit), or -1 on error.
Child uses _exit; parent uses waitpid then WEXITSTATUS.
#include <unistd.h>
#include <sys/wait.h>
int spawn_exit(int code) {
/* TODO */
return -1;
}
Creating zombie processes by failing to reap child with wait/waitpid; forgetting that code after successful execvp() is never reached.
Fork failure (EAGAIN); child process crashing with signal; parent terminating before child; waitpid() with WNOHANG on running child.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.