Copy-on-Write Forks and Process Page Tables
Copy-on-write (COW) fork creates a new process whose virtual memory initially points at the same physical memory as the parent. The operating system lets both processes read those pages, but delays making separate copies until one process tries to write.
That makes fork() relatively inexpensive, especially when the child will soon replace itself with another program using exec. The system does not immediately duplicate the parent’s entire memory. It duplicates the descriptions of the mappings and copies only the pages that must become independent.
The problem with copying everything
A process uses virtual memory: addresses that its program reads and writes, such as a heap pointer or a stack address. The processor’s memory-management hardware translates those virtual addresses into physical pages, fixed-size blocks of actual RAM, using a page table, a data structure containing those translations and their access permissions.
Suppose a process has 1 GB of memory mapped. A traditional fork could allocate 1 GB of new physical memory and copy every byte before allowing the child to run. That is expensive in both time and memory, even if the child immediately calls exec and discards all of that memory.
COW changes the timing. After the fork, parent and child temporarily use the same physical pages. A page is copied only if one process attempts to modify it.
The important distinction is that the processes share physical page contents, not one process’s identity or address space. Each process still has its own page-table structures and its own virtual address space.
What fork changes in the page tables
When a Linux process calls fork(), the kernel creates a child process and gives it a virtual-memory layout equivalent to the parent’s at that moment. The kernel creates page-table structures for the child that describe the same physical pages as the parent’s entries.
For mappings that can be written, the kernel changes the relevant page-table entries—usually called PTEs, or page-table entries—so that both processes temporarily see the page as read-only. The physical page is also tracked as being referenced by both mappings. The parent’s mapping must be made read-only too; otherwise the parent could write without generating the fault that triggers copying.
This is a permission change in the translation, not necessarily a change to the program’s declared mapping. From the program’s point of view, its heap or stack is still writable. The processor will discover the temporary restriction only when a write occurs.
Pages that were already read-only, such as code pages, can normally be shared without this special COW transition. The same physical code page can safely serve both processes because neither is allowed to modify it.
The kernel must also make the processor forget cached translations that still say a page is writable. These cached translations are held in the TLB (translation lookaside buffer). Invalidating or updating the relevant TLB entries prevents a stale permission from bypassing COW.
What happens on a write
Consider a parent and child that both read virtual address 0x400000. Their page tables point that address at the same physical page. Either process can read it normally.
Now the child executes a store instruction that changes a byte on the page:
- The processor looks up the virtual address and sees a read-only page-table entry.
- Because the operation is a write, the processor raises a page fault—a controlled exception reporting that the access cannot currently proceed.
- The kernel examines the fault. It determines that this is a legitimate COW write, rather than an invalid access such as writing to a genuinely read-only code page.
- The kernel allocates a new physical page.
- It copies the old page’s contents into the new page.
- It changes the child’s page-table entry to point to the new page and marks that entry writable.
- It updates the relevant accounting and translation caches, then retries the interrupted instruction.
The retried store now succeeds on the child’s private page. The parent still points to the original page and sees the original contents. If the parent later writes, it goes through the same process.
The unit of copying is normally a whole memory page, not just the individual byte or object being changed. Consequently, a one-byte write can cause a page-sized allocation and copy. If several writes affect the same page after the first fault, they do not require repeated copies: the process already has a private writable mapping.
If the other process has stopped referencing the original page, the kernel may be able to make a remaining mapping writable without copying. The observable rule remains the same: a process must never modify a physical page that is still shared with another process that should see the old contents.
Why process creation starts cheaply
Immediately after fork(), the kernel has done work: it created a process description, created or copied page-table structures, adjusted reference counts, and changed permissions. But that work is much smaller than copying the contents of every mapped page. Physical memory for private copies is allocated only for pages that are actually written.
This is particularly useful for the common pattern where a server forks a child and the child soon calls exec. exec replaces the child’s program and memory mappings, so copying the parent’s old heap and stack would have been wasted. With COW, the child can discard those shared mappings without first paying to duplicate them.
COW is not free. Large page tables still take time to duplicate, page-table memory consumes RAM, and the first write to each shared page incurs a fault and a page copy. But it turns fork’s initial cost from “copy all process memory” into roughly “copy mapping metadata now, copy modified pages later.” That deferred work is why a forked process can begin running quickly while still receiving an independent memory view.