Appearance
2.2.2 — fork, exec & wait
Our photo-upload service does not resize photos itself. For each photo, it starts a separate program that makes the thumbnail, waits for it to finish, and checks whether it worked. That is a sensible design. If the image program crashes on a broken photo, only that program dies. The upload service carries on.
Here is the upload service doing exactly that in TypeScript. To keep it runnable anywhere, the "thumbnail program" is a tiny Node script that reports who it is and then exits.
ts
// make-thumbnail.ts — the upload service starts a separate program for each thumbnail
import { spawn } from "node:child_process";
// Stand-in for a real image tool: a tiny Node program that "resizes" and reports.
const thumbnailer = `
console.log("thumbnailer: my PID is " + process.pid + ", my parent is " + process.ppid);
process.exit(process.argv[1] === "photo-001.jpg" ? 0 : 2);
`;
function makeThumbnail(photo: string): Promise<number> {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, ["-e", thumbnailer, photo], { // (1)
stdio: "inherit", // (2)
});
console.log(`service: started child with PID ${child.pid}`); // (3)
child.on("error", reject); // (4)
child.on("exit", (code) => resolve(code ?? -1)); // (5)
});
}
console.log(`service: my PID is ${process.pid}`);
console.log("exit code:", await makeThumbnail("photo-001.jpg"));
console.log("exit code:", await makeThumbnail("missing.jpg"));
// Prints (PIDs differ on every run):
// service: my PID is 6572
// service: started child with PID 12544
// thumbnailer: my PID is 12544, my parent is 6572
// exit code: 0
// service: started child with PID 25848
// thumbnailer: my PID is 25848, my parent is 6572
// exit code: 2(1)spawnstarts a new process.process.execPathis the path of thenodeprogram itself. The arguments tell that newnodeto run our little script with the photo name.(2)stdio: "inherit"means the child uses the same screen as the parent, so its message appears in our output. Page 2.2.4 shows the other choices.(3)The child is a new process with its own PID. Notice in the output that the child reports our PID as its parent.(4)Theerrorevent fires if the program could not be started at all, for example if the file does not exist.(5)Theexitevent fires when the child finishes. It carries the child's exit code: 0 means success, and any other number means some kind of failure. Our stand-in returned 2 for the missing photo.
That is the friendly surface. Underneath, on Linux and macOS, "start a new program" is built from three system calls that work in a surprising way: fork, exec and wait. This page takes them apart.
1. The puzzle: where does a new process come from?
Every process has a parent (2.2.1). PID 1 is started by the kernel at boot (2.1.3), and everything else descends from it. So how does an existing process make a new one?
You might expect a single system call: "create a process running this program". Windows works that way. Its CreateProcess call takes the name of a program file and starts it as a new process in one step.
Unix, since 1970, does it in two separate steps, and you combine them:
- fork makes a copy of the calling process.
- exec replaces the program running inside a process with a different program.
It sounds odd the first time. Then you see how much freedom the split gives you, and why Unix has kept it for more than fifty years.
2. fork: one process becomes two
When a process calls fork(), the kernel makes a near-exact copy of the calling process. The copy has the same code, the same memory contents, the same open files, and is at the same point in the program. The original is called the parent. The copy is called the child.
From that moment they are two independent processes. They have separate memory. If the child changes a variable, the parent's copy of that variable does not change. It is like a cell dividing: one becomes two, and each then lives its own life.
Called once, returns twice
Here is the part that bends people's minds. fork() is called once, but it returns twice: once in the parent and once in the child. Both processes continue from the same line, the line right after the call. The only difference between them is the number fork() hands back:
- in the parent,
fork()returns the child's PID, a positive number; - in the child,
fork()returns 0; - if the kernel could not make a copy, it returns −1 in the parent, and there is no child.
That return value is how each process discovers which one it is. Here is a complete C program that does what our upload service did: start a thumbnail program and wait for it. It uses ImageMagick's convert, a widely used command-line program that resizes images. It is Linux and macOS code, and it cannot run on Windows, which has no fork.
c
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
printf("parent: my PID is %d\n", getpid());
pid_t pid = fork(); // (1)
if (pid == 0) { // (2) the child
execlp("convert", "convert", "photo-001.jpg", // (3)
"-resize", "200x200", "thumb-001.jpg", (char *)NULL);
perror("exec failed"); // (4)
_exit(127);
} else if (pid > 0) { // (5) the parent
int status;
waitpid(pid, &status, 0); // (6)
if (WIFEXITED(status))
printf("parent: child %d exited with %d\n", pid, WEXITSTATUS(status));
} else {
perror("fork failed"); // (7)
}
return 0;
}If you have not read C before, it is simpler than it looks. pid_t is just the name of the type used for process IDs, so read it as "a number". printf prints text, and %d is replaced by a number. Lines starting // are comments. Now the marked lines:
(1)Callfork()and store whatever it returns inpid. After this line there are two processes running this same code, each with its own variablepidholding a different value.(2)In the child,fork()returned 0, so only the child enters this branch.(3)The child callsexecto turn itself into theconvertprogram, passing it the arguments.execlpis one member of a family ofexecfunctions. Thepmeans "search the directories inPATHfor the program", and thelmeans "the arguments are given as a list". The list must end withNULL.(4)Ifexecworks, it never returns, because the code that called it no longer exists. So this line runs only ifexecfailed, for example becauseconvertis not installed. The child prints why and exits with 127, the usual code for "command not found".(5)In the parent,fork()returned the child's PID, a positive number, so only the parent enters this branch.(6)The parent callswaitpidto sleep until that child finishes, then reads its exit code out ofstatus.(7)Iffork()failed, there is no child at all. This happens when the system has run out of PIDs or memory, or the user has hit a limit on how many processes they may run.
One line of code, fork(), runs in one process and comes out in two, which then take different paths. Every new program on a Unix system starts this way.
3. exec: same process, new program
A copy of yourself is rarely what you want. Usually you want to run a different program, and that is the second step. exec is properly the execve system call, plus a family of library functions around it. It replaces the whole contents of the calling process's memory with a new program read from disk. Then it starts running that program from the beginning.
Here is what changes and what stays when a process calls exec:
| Replaced | Kept |
|---|---|
| the code | the PID |
| the data and heap | the parent |
| the stack | open file descriptors |
| the program counter | the current directory |
| signal handlers | the user it runs as |
The left column is everything about which program is running. The right column is everything about which process this is. The cook stays the same. The recipe changes.
The kept file descriptors matter a great deal. Because the child keeps its open descriptors through exec, the parent can set up the child's standard input and output before the exec, and the new program inherits them without knowing anything about it. That is exactly how a shell sends one program's output into a file or into another program (2.8.1). The one exception is a descriptor marked close-on-exec. The kernel closes those automatically at exec, so that private files of the parent do not leak into programs it starts. Node marks almost all of its own descriptors this way, which is the O_CLOEXEC flag you saw in the strace output on 2.1.2.
4. fork, then exec: how every command is launched
Put the two together and you have the fork–exec pattern. It is how every program on a Unix system is started, including every command you type.
ls. ① The shell forks, and now there are two shells. ② The child calls exec and becomes ls, keeping its PID. ③ When ls exits, the parent's wait returns with the exit code, and the shell prints the next prompt.Follow the numbers. When you type ls and press Enter:
- The shell (PID 400) calls
fork(). Now there are two shells. The parent is still PID 400. The child is a new copy, PID 401. - The child calls
exec("ls"). Its memory is replaced with thelsprogram. It is still PID 401, still the shell's child, and it still has the shell's screen as its output, solsprints to your terminal. lsdoes its work and exits with code 0. The parent shell, which was sleeping inwait(), wakes up with that code. Then it prints the next prompt.
Why split it into two steps?
Because of the gap between fork and exec. In that gap, the child is still running the parent's code, so it can prepare its own environment before it becomes the new program. For example, the child can:
- point its standard output at a file, to make
ls > files.txtwork; - connect its output to another program, to make
ls | sortwork; - change its current directory;
- lower its own priority, or drop some of its permissions.
The new program then starts with all of that already arranged, and needs no code of its own to support any of it. A single "create process" call would need an option for every one of those things. Windows's CreateProcess does in fact take a large structure of options for this reason.
5. fork is cheap because it does not really copy
There is an obvious objection. Our upload service might use 500 MB of memory. Does fork() copy 500 MB, only for the child to throw it all away a microsecond later with exec? That would be terribly slow.
It does not, because of copy-on-write, often shortened to COW. After fork(), the kernel does not copy the parent's memory. Instead, parent and child both point at the same physical memory, and the kernel marks all of it read-only for both of them.
As long as both processes only read, they share the memory and nothing is copied. The moment either process tries to write to a piece of memory, the CPU raises an exception, because that memory is marked read-only. The kernel catches it, makes a private copy of just that one small piece (one page, usually 4 KB, 2.5.1), gives it to the process that wanted to write, and lets the write continue.
If the child calls exec straight away, it writes almost nothing before its memory is replaced. So almost nothing is ever copied, and fork costs a tiny fraction of what a real copy would. This is a pattern you will meet again and again in this book: do not do expensive work until someone actually forces you to. File systems use the same trick for instant snapshots (2.6.2), and container images use it for their layers (14.1.3).
Some programs use fork without exec, on purpose. The Redis database (7.6) saves a snapshot of its memory to disk this way. It forks a child. The child sees the memory exactly as it was at the moment of the fork, even while the parent keeps changing its own copy, and writes that frozen picture to disk at its own pace. Copy-on-write makes this nearly free, except for the pages the parent changes during the save.
Even cheaper: vfork and posix_spawn
For the common case of "fork and then immediately exec", even copy-on-write does some unnecessary work, because the kernel still has to build a new memory map for the child. Two alternatives skip it:
vforkcreates the child without any memory of its own. The child borrows the parent's memory, and the parent is paused until the child callsexecor exits. It is fast and dangerous, because the child could damage the parent's memory if it does anything exceptexec.posix_spawnis a single library call that means "start this program as a new process, with these settings", much like Windows'sCreateProcess. The library picks the fastest safe method underneath.
You rarely choose between these yourself. Node's spawn, Python's subprocess and similar tools pick one for you. What matters is the model: a new process is a copy of its parent that then becomes something else.
6. wait, zombies and orphans
In the C program, the parent called waitpid to collect the child's exit code. That call does two jobs:
- It tells the parent how the child ended: which exit code it returned, or which signal killed it.
- It tells the kernel the parent has the information, so the kernel can throw away the child's last record.
Why is there a "last record" at all? Because when a process finishes, the kernel frees its memory and closes its files at once, but it cannot forget the process entirely. Its parent might still ask for the exit code. So the kernel keeps a tiny entry, holding the PID and the exit code, until the parent collects it. Collecting it is called reaping the child.
Zombies
A zombie is a child that has finished but has not been reaped yet. It is dead, but its entry is still in the process table. ps shows it with state Z, often labelled <defunct>.
A few zombies for a moment are completely normal. Every process is briefly a zombie between exiting and being reaped. The trouble comes when a buggy parent never calls wait. Each finished child then leaves a zombie behind forever. A zombie uses no memory and no CPU, but it does keep its PID. If the parent starts thousands of children and never reaps them, the system can run out of PIDs, and then fork fails for everyone.
You cannot kill a zombie, because it is already dead. kill -9 does nothing to it. The fix is to make the parent reap its children, or to end the parent, which turns the zombies into orphans.
Orphans
An orphan is a process whose parent died first. The kernel cannot leave it without a parent, because someone must reap it when it finishes. So the kernel re-parents it, normally to PID 1. One of PID 1's standing duties is to call wait regularly and reap any orphans it has adopted.
This is one reason PID 1 is special (2.1.3). It also explains a classic container problem. Inside a container, the first program you start becomes PID 1. If that program is our upload service, and it was never written to reap adopted orphans, then every orphan in the container turns into a zombie when it finishes, and the zombies pile up. Page 2.2.3 covers the fix, a tiny program built to be PID 1.
In Node, you never call wait yourself. Node reaps every child it starts and turns the result into the exit event you saw in line (5) of the first snippet. That is why Node programs rarely leak zombies, unless something outside Node starts processes.
7. Two traps worth knowing
Forking a program that has threads
Threads are several strands of work inside one process, sharing its memory (2.3.1). fork and threads combine badly. When a process with several threads calls fork, the child gets a copy of all the memory, but only one thread: the thread that called fork. The other threads simply do not exist in the child.
Now suppose another thread was in the middle of allocating memory at the moment of the fork, and was holding the memory allocator's lock (2.4.2 explains locks). The child gets a copy of that lock in its locked state. But the thread that would have released it does not exist in the child. So the first time the child tries to allocate memory, it waits for the lock forever. The child hangs, and only sometimes, depending on exact timing, which makes it horrible to debug.
The safe rule is: in a program with threads, call exec straight after fork, and do almost nothing in between. exec throws away the copied memory, including the stuck lock. Python learned this lesson publicly. Its multiprocessing module used plain fork by default on Linux for years, and programs that also used threads sometimes hung. In Python 3.14 the default was changed to a safer method.
Output that appears twice
Here is a puzzle people meet in their first week with fork. A program prints "starting" without a newline, then forks, then both processes exit normally. The word starting appears twice. Why?
Because printf did not send the text to the kernel yet. It put it in a buffer inside the program's memory, to send later in one bigger write (2.1.2 explained why programs buffer). fork copied the program's memory, buffer included. When each process exits, it flushes its own copy of the buffer, so the text is written twice. The fix is to flush output before forking. Our C program avoids the problem in two ways: its text ends in a newline, which flushes a terminal's buffer, and its child either calls exec or exits with _exit, which does not flush.
What the interviewer will push on
"Explain fork and exec." They are checking the model, not the syntax. A good answer says fork copies the calling process and returns twice, with the child's PID in the parent and 0 in the child, and exec replaces the program inside a process while keeping its PID and open descriptors. The tell is explaining why the two are separate: the child can set up redirection and other settings in the gap. The common weak answer describes fork as "creating a thread".
"Doesn't fork copy all the memory? Isn't that slow?" They want copy-on-write: shared read-only pages, copied one page at a time only when written. A strong answer adds the Redis snapshot as a real use of fork without exec.
"What is a zombie process, and how do you get rid of one?" They are checking that you know a zombie is already dead, so killing it does nothing. The fix is the parent: make it reap, or end it so PID 1 adopts and reaps the zombies. The common wrong answer is kill -9 on the zombie.
"What happens to a child when its parent dies?" It becomes an orphan and is re-parented, normally to PID 1, which reaps it. Bonus: in a container, your own program may be PID 1 and may not do this.
"Is it safe to fork a program that uses threads?" This is a senior question. The answer: only one thread survives in the child, so locks held by other threads stay locked forever. Call exec immediately, or use posix_spawn.
Something to volunteer that nobody asks for: the reason ls > files.txt needs no support inside ls is the gap between fork and exec. The shell's child redirects its own output there, and ls simply inherits it. That one observation shows you understand why Unix split process creation in two.
Recall
- fork copies the calling process. It is called once and returns twice: the child's PID in the parent, 0 in the child, −1 on failure.
- exec replaces the program inside a process. Code, heap and stack are replaced; PID, parent and open file descriptors are kept, except those marked close-on-exec.
- fork–exec launches every Unix program. The gap between the two calls is where the child sets up redirection, pipes and other settings.
- copy-on-write makes fork cheap: pages are shared read-only and copied one at a time only when written.
vforkandposix_spawnare faster still for fork-then-exec. - The parent must reap each child with
wait. A finished, unreaped child is a zombie that holds its PID. A child whose parent dies is an orphan and is adopted and reaped by PID 1. Forking a program with threads is dangerous because only one thread survives in the child.
Self-test: How do the parent and the child each know which one they are after fork? What does exec keep, and why does keeping the file descriptors matter? Why is fork cheap even for a 500 MB process? Why can't you kill a zombie, and what actually removes it? Why can a forked child of a program with threads hang?
Quiz Bank
FoundationalWhat do fork() and exec() each do?
fork creates a new process that is a near-exact copy of the caller: same code, same memory contents, same open files, at the same point in the program. It returns twice, giving the child's PID to the parent and 0 to the child.
exec replaces the program running inside the current process with a new program read from disk, starting it from the beginning. The process keeps its PID, its parent and its open file descriptors, but its code, data, heap and stack are all replaced.
Together, as fork–exec, they start every program on a Unix system: copy yourself, then turn the copy into the program you want.
FoundationalHow does a process know whether it is the parent or the child after fork?
From the value fork() returns. Both processes continue from the line after the call, but in the parent fork() returns the child's PID, a positive number, and in the child it returns 0. If the kernel could not create the child, the parent gets −1 and there is no child. Code checks that value with an if to send each process down its own path.
AppliedWhy is fork fast even when the parent uses hundreds of megabytes of memory?
Because of copy-on-write. The kernel does not copy the parent's memory. It points the child at the same physical pages and marks them read-only for both. Only when one of them writes to a page does the CPU raise an exception, and the kernel copies just that page for the writer.
When the child calls exec almost immediately, as it usually does, it writes very little before its memory is replaced, so almost nothing is copied. Calls such as vfork and posix_spawn avoid even the cost of setting up the shared memory map.
InterviewWhat is a zombie process, and how do you get rid of one?
A zombie is a child that has finished but whose parent has not yet collected its exit status with wait. The kernel has freed its memory and files, but keeps a small entry with its PID and exit code for the parent. ps shows it as Z or <defunct>.
A zombie cannot be killed, because it is already dead. A few for a moment are normal. Many that stay mean the parent has a bug and never reaps. To remove them, fix the parent so it calls wait, or end the parent. Then the zombies become orphans, PID 1 adopts them, and PID 1 reaps them at once. Long-lived zombies matter because each one holds a PID, and running out of PIDs makes fork fail for everyone.
InterviewWhat is an orphan process, and what happens to it?
An orphan is a process whose parent has exited while it is still running. The kernel re-parents it, normally to PID 1, so that someone will collect its exit status when it finishes. PID 1 regularly calls wait to reap such adopted children.
This is one reason PID 1 has a special duty. Inside a container, the first program started becomes PID 1, and if it was not written to reap adopted children, orphans that finish turn into zombies that pile up.
StaffWhy is calling fork() in a program with several threads dangerous, and what is the safe pattern?
fork copies the whole memory but only the calling thread. Every other thread vanishes in the child. Any lock those threads held at that instant is copied in its locked state, with no thread left in the child to release it. Data structures they were in the middle of changing are copied half-changed.
If the child then calls anything that takes such a lock, for example malloc, which locks internally, it waits forever. The hang depends on exact timing, so it appears rarely and is very hard to reproduce.
The safe pattern is to call exec immediately after fork, doing nothing in between except a few simple, safe calls, because exec discards the copied memory and its stuck locks. Better still, use posix_spawn, or a tool such as Node's spawn that does this correctly for you. If you need worker processes, start them before creating any threads. Python changed its multiprocessing default away from plain fork in 3.14 because of exactly this problem.
Scenario Drill
DrillThe photo-upload service runs in a container. After a few days, ps inside the container shows thousands of entries marked defunct, and eventually new thumbnails fail with an error saying a resource is temporarily unavailable. Explain what happened and how to fix it.
The defunct entries are zombies. The likely story: the thumbnail tool itself starts helper programs, and sometimes the tool exits before its helpers do. Those helpers become orphans. The kernel re-parents orphans to PID 1. But inside the container, PID 1 is our upload service, the first program the container started, and it was never written to reap children it did not start itself. So each orphan becomes a zombie when it finishes, and nothing ever removes it.
Each zombie keeps its PID. After a few days the container has used up the PIDs it is allowed. Then fork fails with EAGAIN, "resource temporarily unavailable", and no new thumbnail program can start.
The fix is to give the container a proper PID 1. A tiny init program such as tini, or Docker's --init option, runs as PID 1, starts the upload service as its child, reaps every orphan it adopts, and passes signals on to the service. Page 2.2.3 explains the signal half of that job.
Next: the parent's wait told it how the child ended, and one way a process can end is by being sent a signal. Signals are how the kernel, the terminal and other programs tap a running process on the shoulder. They are also the machinery behind every graceful shutdown during a deploy. Page 2.2.3 explains them.