Before fork
fork() must give the child its own copy of the parent's memory. Copying every page at once is slow, and often wasted: many children call exec(), which loads a new program, and throw the copy out. Copy-on-write puts off each copy until someone writes.
Each process has a page table. An entry names the frame of physical memory holding a page, and whether writes are allowed. The parent's code page sits in frame 4, read-only (R); its data page, with x = 1, in frame 5, writable (RW).
Sharing, not copying
Line 1 calls fork(). The kernel gives the child a new page table and copies the parent's entries into it, not the pages. Both tables now point at the same two frames. Not one byte of the program's memory was copied.
Before fork returns, the kernel clears the write bit on the data page in both tables: 5 · RW becomes 5 · R. It also notes that frame 5 now has two users. Either process may read it; neither may write.
Reads need no help. Both processes read x = 1 straight from frame 5 and run the same code from frame 4. The code page was read-only from the start, so it stays shared for good: two processes, one copy.
The first write
The child runs line 2, x = 2. The CPU's memory unit (MMU) looks up the child's entry for the data page: frame 5, read-only. It refuses the store before any byte changes and raises a page fault into the kernel.
The kernel sees a copy-on-write page, not a bug. It takes free frame 6, copies all 4096 bytes of frame 5 into it, and points the child's entry at frame 6 with writing allowed: 6 · RW.
The kernel returns from the fault, and the CPU runs the same store again. This time the entry allows it, so frame 6 now holds x = 2. Frame 5 still holds x = 1, and only the parent uses it.
The child prints x=2 and exits; the parent, done waiting, prints x=1. Same address, different frames: each process saw only its own writes, exactly as if fork had copied everything.
Why it wins
Only pages that are written get copied, one at a time, when it happens. A child that calls exec() at once copies almost nothing. The same trick backs private file mappings, and Linux KSM merges identical pages into one write-protected copy.
What happens
;; step 1/10 · copy-cost
the whole program (C99, POSIX):
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int x = 1;
int main(void) {
pid_t pid;
pid = fork();
if (pid == 0) x = 2;
else wait(NULL);
printf("x=%d\n", x);
return 0;
}
fork() child gets a copy of the address space
exec() often called next: replaces it all
COW copy a page only when it is written
Frame numbers 4, 5 and 6 are made up for the picture: a real kernel takes whatever frames its free list hands out. A real process also has many more pages (stack, heap, libraries); the lesson shows the two that matter. In a real fork the kernel copies each entry and clears its write bit in the same pass; the figure shows the copy and the write-protect as two steps. Which process runs first after fork is not fixed: here the parent's wait() makes the child print first.