Process creation and exec
Quick answer
fork creates a child process; exec replaces the program image of an existing process; wait collects a child’s termination status. These operations form the basis of many shells and process supervisors.
A successful exec does not return to the original program; the existing process continues with a replacement image. The parent must still collect the child's result. Distinguish a failed exec from a child that successfully started but returned a nonzero status.
Prepare the practice environment
Run python3 lab-fixtures/syscalls/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Examples use Python 3 standard-library wrappers on Linux, private files, bounded child processes and local socketpairs. No root or external network is needed. Optional strace inspection requires an authorized environment. Review the lab guide.
Prerequisites
Review: File descriptors and errno.
How it works
fork returns 0 in the child and the child PID in the parent. Linux uses copy-on-write for ordinary private memory; inherited descriptors can refer to the same open file descriptions. Successful execve does not return and normally keeps the PID while replacing memory and executable code; descriptors marked close-on-exec are closed. The parent should reap children using waitpid and decode normal exit versus signal termination. A zombie is an exited child whose termination status has not been collected, not a running task. A failed exec must be handled in the child, which should exit without repeating the parent’s buffered cleanup. These samples fork only a single-threaded toy process. In multithreaded C programs, only async-signal-safe operations are allowed in a post-fork child before exec; prefer an appropriate spawn API for complex applications. See fork(2), execve(2), wait(2), posix_spawn(3).
Try the examples
python3 - <<'PYCODE'import ospid = os.fork()if pid == 0: os._exit(7)child, status = os.waitpid(pid, 0)assert child == pid and os.WIFEXITED(status)print('child exit:', os.WEXITSTATUS(status))PYCODEThe child exits with 7; the parent reaps it and decodes the wait status.
Expected stdout:
child exit: 7python3 - <<'PYCODE'import ospid = os.fork()if pid == 0: try: os.execve('/bin/echo', ['echo', 'exec replaced image'], {}) except OSError: os._exit(127)child, status = os.waitpid(pid, 0)assert child == pid and os.waitstatus_to_exitcode(status) == 0print('parent reaped child')PYCODEexec runs a fixed trusted executable with an explicit argument vector and empty environment. Output order is established by waitpid.
Expected stdout:
exec replaced imageparent reaped childpython3 - <<'PYCODE'import os, errnopid = os.fork()if pid == 0: try: os.execve('./absent-program', ['absent-program'], {}) except OSError as e: os._exit(127 if e.errno == errno.ENOENT else 1) os._exit(1)_, status = os.waitpid(pid, 0)assert os.waitstatus_to_exitcode(status) == 127print('missing executable handled')PYCODE127 is a convention selected by this program, not a direct execve return value.
Expected stdout:
missing executable handledCommon mistakes
Exec is not another fork. Never use wait status as the exit code without decoding it. Avoid fork after starting threads in Python.
Practice
Change the child exit code to 3 and adjust the assertion. Explain who executes each branch and why the successful exec branch cannot continue.
Related references
Study: strace, ps. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Process creation and exec — Knowledge Check
Apply Linux systems programming interfaces to concrete local examples.
What does fork return to the child?
What does successful exec normally do to the PID?
Why call waitpid?
What is a zombie?