Skip to content

Volume ITechnology

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) spawn starts a new process. process.execPath is the path of the node program itself. The arguments tell that new node to 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) The error event fires if the program could not be started at all, for example if the file does not exist.
  • (5) The exit event 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:

  1. fork makes a copy of the calling process.
  2. 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) Call fork() and store whatever it returns in pid. After this line there are two processes running this same code, each with its own variable pid holding a different value.
  • (2) In the child, fork() returned 0, so only the child enters this branch.
  • (3) The child calls exec to turn itself into the convert program, passing it the arguments. execlp is one member of a family of exec functions. The p means "search the directories in PATH for the program", and the l means "the arguments are given as a list". The list must end with NULL.
  • (4) If exec works, it never returns, because the code that called it no longer exists. So this line runs only if exec failed, for example because convert is 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 calls waitpid to sleep until that child finishes, then reads its exit code out of status.
  • (7) If fork() 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:

ReplacedKept
the codethe PID
the data and heapthe parent
the stackopen file descriptors
the program counterthe current directory
signal handlersthe 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.

shellPID 4001fork()shell (parent)PID 400, waitscopy of shell (child)PID 4012exec("ls")lsstill PID 4013exits with code 0 →parent's wait() returns
Figure 1 — How the shell runs 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:

  1. 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.
  2. The child calls exec("ls"). Its memory is replaced with the ls program. It is still PID 401, still the shell's child, and it still has the shell's screen as its output, so ls prints to your terminal.
  3. ls does its work and exits with code 0. The parent shell, which was sleeping in wait(), 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.txt work;
  • connect its output to another program, to make ls | sort work;
  • 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.

just after forkparentchildpage 1page 2page 3all three pages shared, all read-onlyafter the child writes to page 2parentchildpage 1page 2page 3page 2′only the written page was copied
Figure 2 — Copy-on-write. Left: right after fork, parent and child point at the same physical pages, marked read-only. Right: when the child writes to page 2, the kernel copies only that page and gives the copy to the child. Pages 1 and 3 are still shared.

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:

  • vfork creates the child without any memory of its own. The child borrows the parent's memory, and the parent is paused until the child calls exec or exits. It is fast and dangerous, because the child could damage the parent's memory if it does anything except exec.
  • posix_spawn is a single library call that means "start this program as a new process, with these settings", much like Windows's CreateProcess. 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:

  1. It tells the parent how the child ended: which exit code it returned, or which signal killed it.
  2. 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. vfork and posix_spawn are 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.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet