How Does Copy-on-Write Enable Fast Process Creation?

Copy-on-write is a memory management technique that allows an operating system to create new processes rapidly by sharing existing physical memory pages between parent and child processes instead of copying them immediately. Both processes read from the same physical frames until one attempts a write, at which point the kernel transparently duplicates only the modified page.

Virtual Memory and Page Tables

In classical computing, processes operate in isolated virtual address spaces rather than accessing physical RAM directly. The Memory Management Unit (MMU) translates virtual addresses into physical addresses using per-process page tables. Each page table entry (PTE) maps a virtual page to a physical frame and stores permission bits such as read, write, and execute.

Because virtual addresses are decoupled from physical frames, multiple page tables can point to the exact same physical memory frame. Copy-on-write (COW) exploits this indirection to avoid eager memory duplication during process creation.

What Happens During Process Creation

When a process invokes a system call such as fork() in Unix-like operating systems, the kernel must create a child process with an identical view of memory. Without copy-on-write, the kernel would have to allocate new physical frames for every page in the parent's address space and copy all contents byte by byte.

For a process using \(N\) pages of size \(P\), eager copying requires \(\mathcal{O}(N \cdot P)\) time and doubles physical memory consumption immediately. With copy-on-write, the kernel instead:

  1. Allocates a new page table for the child process.
  2. Copies the parent's page table entries into the child's page table so both point to the same physical frames.
  3. Marks all shared writable pages as read-only in both the parent and child page tables.
  4. Increments a reference count on each shared physical frame.

Because copying page table entries is much smaller than copying the underlying data pages, process creation completes in \(\mathcal{O}(N)\) metadata operations rather than \(\mathcal{O}(N \cdot P)\) data movement.

Handling Writes via Page Faults

As long as both the parent and child only read from shared pages, they access the same physical frames at full hardware speed with zero extra overhead. The deferral mechanism is triggered only when either process attempts to write to a shared page.

When a write occurs on a page marked read-only in the PTE, the MMU raises a protection page fault and traps into the operating system kernel. The kernel's page fault handler inspects the virtual memory area metadata to distinguish a copy-on-write fault from an illegal memory access.

If the fault is a valid copy-on-write event, the kernel checks the reference count of the underlying physical frame:

  • Reference count greater than 1: The kernel allocates a new physical frame, copies the contents of the original page into it, updates the faulting process's PTE to point to the new frame with write permissions enabled, and decrements the original frame's reference count.
  • Reference count equal to 1: No other process shares the frame anymore, so the kernel simply restores write permissions in the PTE without copying anything.

After updating the page table and invalidating the corresponding Translation Lookaside Buffer (TLB) entry, the kernel resumes the faulting instruction, which now succeeds normally.

Why Copy-on-Write Is Effective in Practice

Copy-on-write is especially effective because of two common patterns in classical computing:

  • Immediate exec() after fork(): Shells and runtime environments frequently spawn a child process only to replace its entire address space with a new program via exec(). Copying the parent's memory would be completely wasted work.
  • Sparse modifications: Even when a child process continues running alongside the parent, it typically modifies only a small fraction of its total memory, such as stack frames or specific heap objects, leaving large read-only or untouched regions shared indefinitely.

By deferring physical duplication until the exact moment of mutation, copy-on-write minimizes both process startup latency and overall physical memory pressure.