fork: one process in, two out.
You tap Instagram on your phone. Or, on a laptop, you open a terminal (the window where you type commands) and type ls, the command that lists your files. Either way, a new process appears a moment later (you met processes in the Processes lesson).
But where does a new process come from? Something has to make it.
See it#
Look back at the picture at the top. A running process doesn't build a new one from nothing. It makes a copy of itself, and for a moment there are two identical processes running the same code from the same line.
Then one of the two changes what it runs. That's the whole trick, in two moves: copy, then swap the program.
Name it#
Those two moves are two system calls (What is an OS). Creating a process on Linux, macOS and Android means calling fork, which copies the running process, and then exec, which replaces the copy's program with a new one.
The original is called the parent and the copy is its child. Each has its own number, its process ID or PID (the ID from Processes). On Linux, macOS and Android, every process was made this way, by a parent that forked, and that parent had a parent of its own, all the way back to the first process the OS starts. (Windows makes a new process in one call instead, like Java's ProcessBuilder in the Code below.)
How it works#
1. Fork makes a copy#
When a process calls fork(), the OS makes a child that's a copy of it: the same code, the same memory, the same open files. Both carry on from the line right after fork().
So how does each one know which it is? By what fork() hands back:
| Who | What fork() returns |
|---|---|
| The child | 0 |
| The parent | the child's process ID, its PID |
| The parent, if the copy failed | -1, and there's no child |
The code checks that value and branches: "if it's 0, I'm the child, do this; otherwise I'm the parent, do that".
Shared until one of them writes.
Copying a whole process's memory would be slow, and most children throw it away a moment later anyway. So the OS doesn't copy at once. Memory is kept in small blocks called pages. Parent and child share the same pages, marked read-only, and a page is copied only when one of them writes to it. This is copy-on-write, and it's why fork() is fast even for a big process.
Either way, after a fork they are two separate processes. If the child changes a variable, it changes its own copy: the parent never sees it. That's the opposite of threads, which share one memory (Threads).
2. Exec swaps the program#
exec: same PID, new program.
A copy of the shell (the program in the terminal that reads your commands) isn't much use on its own. To run ls, the child calls exec, which throws away its code, data, heap and stack and loads the ls program in their place.
Three things are true right after an exec that works:
- the process is the same one, with the same PID: no new process is created;
- it now runs the new program from its first line;
- the old code is gone, so
execnever returns to it. A line after a successfulexecnever runs.
That's how your terminal runs every command: the shell forks, the child execs the command, and the parent waits for it to finish before showing the prompt (the line waiting for your next command) again.
3. How many times does this print?#
Three forks make eight processes.
Every process that reaches a fork() splits in two, so each fork() doubles the count. One fork() makes 2 processes, two make 4, and three make 8: the original plus 7 new children. Read what the question asks. In total there are 8 processes. New children, not counting the original, are 7.
They also like to put prints between the forks, the way they write it in C:
Count the processes at each print. After one fork there are 2 processes, so A prints 2 times. After the second fork there are 4, so B prints 4 times. That's 6 lines in all, not 8: the 2 new children from the second fork were born after the A line, so they never print it.
4. Zombies and orphans#
Finished, but still in the table.
When a child finishes, it doesn't vanish at once. The OS keeps a small entry for it, holding its exit status (a number saying how it ended, 0 for fine), until the parent asks how it went by calling wait(). A child that has finished but hasn't been waited on yet is a zombie: dead, but still in the process table, the OS's list of every process and its card. One zombie is harmless, but a parent that never waits leaves them piling up, and the table can fill.
Orphans get adopted by init.
The opposite case is a child that's still running when its parent exits. That child is an orphan. It isn't killed: the OS hands it to the first process, init (PID 1), or systemd on most Linux systems today, which adopts it and waits for it when it finishes, so it never stays a zombie.
Why it matters#
This pair runs your whole machine. Every command in a terminal is a fork and an exec. On Android, every app you open is forked from one waiting process, called Zygote, that already has the common code loaded, which is part of why apps start quickly. Web servers have long forked a child per worker, and copy-on-write keeps that cheap.
In interviews it comes up as "What does fork() return?", "How many processes does this code create?" and "What's the difference between a zombie and an orphan?". Their words for what you just saw:
| Interviews call it | What you saw |
|---|---|
fork() return value | 0 in the child, the child's PID in the parent, -1 on failure |
exec family | same PID, new program; never returns on success |
| Process tree | each fork doubles the count: n forks make 2 to the power n processes |
| Zombie process | finished, but the parent hasn't called wait() |
| Orphan process | still running after its parent exits; adopted by init |
Code#
The parent forks, the child prints its PID and then execs the echo program, and the parent waits for it. Java has no fork(), so its version uses ProcessBuilder, which starts a new process running another program in one step, like a fork and an exec together.
A run prints something like this (the PIDs change every time):
The PID the parent prints is the same one the child sees, and it stays the same after the child execs echo.
What to remember#
- A new process is made by fork, which copies the parent, and then exec, which swaps the copy's program for a new one.
- fork returns 0 in the child and the child's PID in the parent. Each fork doubles the processes: three forks make 8.
- exec keeps the same PID, runs the new program, and never returns on success.
- A zombie has finished but its parent hasn't called wait(). An orphan is still running after its parent exits, and init adopts it.
- After a fork, parent and child have separate memory: a change in one doesn't reach the other.