Start here
clone, vfork (dangerous), and posix_spawn.fork / exec (theory)
fork (or clone with flags) creates a child that duplicates much of the parent’s kernel view, often using copy-on-write mappings for efficiency. execve loads a new ELF, replaces the address space, resets signals, and preserves the PID unless you created a new task differently, while carefully deciding which file descriptors survive (CLOEXEC).
By the end of this page you should be able to
- Describe CoW at a high level after fork.
- List two things exec typically resets vs what may persist.
- Explain why locking across fork in a multithreaded parent is hazardous.
Operating systems · Processes
Process creation (fork, exec)
Unix composition for spawning
TL;DR
`fork()` clones the calling task (modern Linux uses `clone` with flags); `execve()` replaces the address space with a new program image. Between them, shells reshape fds (pipes, redirections) before the final exec, COW makes fork cheap until writes happen.
Summary
Fork/exec is the Unix answer to “create a new program with a fresh memory image but inherit selected state.” Understanding fd tables and signal disposition inheritance is essential for secure multi-process servers.
Diagram
Read the diagram definition
flowchart LR
F[fork/clone] --> C[child runnable]
F --> P[parent continues]
C --> E[execve]
E --> N[new image same PID]Deeper narrative
`fork` duplicates task structures with CoW mappings; `execve` replaces the image while often preserving PID. POSIX `posix_spawn` wraps optimized paths (`vfork`+`exec` historically) to reduce latency and locking in multithreaded parents.
Mechanism (step by step)
fork/clone
Duplicate PCB subset, share or copy files/mm per flags, enqueue child as runnable.
Copy-on-write
PTEs marked read-only shared; writes fault to duplicate private pages.
execve
Load new ELF, smash mm, reset signals, keep some fds unless CLOEXEC.
Interpreter chain
Shebang or binfmt_misc may spawn helper before target binary.
Key insights
Return twice
Copy-on-write
Threads complicate fork
Shebang and interpreters
Did you know?
Common pitfalls
Locks held across fork in multithreaded parent
Child gets undefined lock state, use `pthread_atfork` or `posix_spawn`.
Leaking fds
Default fds survive exec unless `O_CLOEXEC` set, security issue for privileged children.
Comparison
fork vs exec
| fork | exec | |
|---|---|---|
| Creates new PID | Yes | No |
| Loads new binary | No | Yes |
| Typical next step in shells | dup2/pipe setup | exec the target binary |
Glossary
CoW
CLOEXEC
ASLR
Interview follow-ups
- Why is `vfork` dangerous and when is it still used?
- What does `exec` preserve from the old process?
- How does `clone` subsume `fork`?
Callouts
vfork caution
`vfork` suspends parent until exec, easy to deadlock if child misbehaves; avoid unless you truly know the platform contract.
Walkthrough & deep dive
Left: practical “how would I use this?” threads. Right: slower, code-leaning explanations, open one panel at a time so the page does not feel like a wall of sameness.
Walkthrough
Buffering puzzle
Deep dive
What is duplicated vs shared?
Address space mappings are duplicated logically with COW; file descriptor table entries are copied pointing at shared `struct file` unless CLOEXEC, offsets are shared unless separated.
Minimal spawn
pid_t p = fork();
if (p == 0) {
execl("/bin/true", "true", (char *)NULL);
_exit(127);
}
waitpid(p, NULL, 0);Self-check
1After successful exec, the program counter points to…
2COW mainly optimizes…
3CLOEXEC marks fds to…