4.2.29Operating Systems

Copy-on-write

1,881 words9 min readdifficulty · medium3 backlinks

WHAT is Copy-on-write?

The "unit" of copying in OS memory is the page (typically 4 KB4\text{ KB}). The trigger mechanism is a page fault caused by writing to a page marked read-only.

Where you meet it:

  • fork() — child shares parent's address space until one writes.
  • mmap(... MAP_PRIVATE) — file pages shared until written.
  • Snapshots in filesystems (ZFS, Btrfs) and VM memory.

WHY does it exist? (the problem it kills)

Two wins:

  1. Speed: fork() becomes O(number of pages)O(\text{number of pages}) for table setup, not O(memory size)O(\text{memory size}) for data copy.
  2. Memory: pages stay shared (deduplicated) as long as nobody writes — great when many processes share read-only code/data.

HOW does it work, step by step?

When a process is duplicated (e.g. fork()):

  1. The child gets its own page tables, but every entry points to the same physical frames as the parent.
  2. All shared writable pages are marked read-only in both page tables, and flagged internally as COW.
  3. The OS keeps a reference count per physical frame: how many mappings share it.
  4. Reading? Fine — read-only allows reads. No fault, no copy.
  5. Writing to a COW page → CPU raises a page fault (write to read-only page).
  6. The fault handler checks: "Is this a COW page?" Yes →
    • Allocate a new frame, copy the old page's contents into it.
    • Point the writer's PTE to the new frame, mark it writable, decrement old frame's refcount.
    • If old refcount drops to 11, the remaining owner's page can be made writable again (no need to keep it COW).
  7. Re-execute the faulting instruction; now the write hits the private copy.
Figure — Copy-on-write

Common mistakes


Recall Feynman: explain to a 12-year-old

You and your friend both want to read the same comic. Instead of buying two comics, you share one. The rule: nobody draws in it. The moment you do want to scribble a mustache on a character, you run to the copier, copy just that page, scribble on your copy, and leave the shared comic clean for your friend. You never copied the whole comic — only the one page you wanted to change. That's copy-on-write: share until you change, then copy only the changed part.


Active recall

What is copy-on-write?
A technique where multiple consumers share one read-only physical copy of data; the first write triggers a copy of only the affected page, after which the writer has a private copy.
What is the unit of copying in OS-level COW?
A memory page (typically 4 KB).
What hardware mechanism detects a COW write?
A page fault, raised because the shared page is marked read-only.
Why does fork() use COW?
Eagerly copying the whole address space is wasteful (often thrown away by an immediate exec()); COW copies nothing up front and only duplicates pages that are actually written.
After fork, who marks the shared pages read-only — child or both?
Both parent and child, so whichever writes first triggers the copy.
What is the COW time cost formula?
Tcow=Nct+WccT_{cow} = N c_t + W c_c, where NN=pages, WW=written pages, ctc_t=table-entry cost, ccc_c=page-copy cost.
When is COW worse than eager copying?
When nearly every page is written (WNW \approx N), since you pay table setup + all copies + per-page fault overhead.
What does the reference count track in COW?
How many mappings currently share a given physical frame.
After a COW page is copied and refcount becomes 1, what happens?
The remaining owner's page can be made writable again (no longer COW), so future writes don't fault.
Does a process that only reads shared pages ever trigger a copy?
No — reads are allowed on read-only pages, so no fault and no copy occur.

Connections

  • fork() system call — primary user of COW.
  • Page tables and PTE flags — read-only / COW bits live here.
  • Page faults — the trap that triggers the copy.
  • Virtual memory — COW is a virtual-memory optimization.
  • Demand paging — sibling "lazy" technique (load on access vs copy on write).
  • mmap and MAP_PRIVATE — file-backed COW.
  • Reference counting — used to know when sharing ends.
  • Snapshots in filesystems (ZFS/Btrfs) — COW at the block level.

Concept Map

shares

copies unit

delays

triggers

handled by

updates

tracks

used by

avoids

gives

no fault

Copy-on-write

Single physical copy read-only

Page 4KB

Copy only on write

Write attempt

Page fault

Fault handler copies page

Writer PTE to new frame

Per-frame reference count

fork child shares parent space

fork then exec waste

Speed and memory savings

Read access

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Copy-on-write ka basic idea simple hai: agar do processes ko same data chahiye, to turant dono ke liye alag-alag copy banane ki zaroorat nahi. Hum ek hi physical copy ko share karte hain aur use read-only mark kar dete hain. Jab tak koi sirf padh raha hai, sab theek — koi copy nahi banti, memory bachti hai. Lekin jaise hi koi process us page pe write karne ki koshish karta hai, CPU ek page fault maarta hai (kyunki page read-only tha), aur OS us waqt sirf us ek page ki nayi copy banata hai. Isiliye naam hai "copy-on-write" — copy tabhi banti hai jab write hoti hai.

Iska sabse bada use fork() mein hai. Purane zamane mein fork() poora address space copy karta tha, lekin aksar child turant exec() karke sab kuch replace kar deta tha — matlab poori copy waste! COW se fork() shuru mein kuch bhi data copy nahi karta, sirf page tables set karta hai. Isse fork bahut fast ho jaata hai, aur jo pages kabhi change nahi hote wo hamesha share rehte hain.

Ek important baat yaad rakho: COW sirf ek optimization hai, program ke liye parent aur child ke address spaces bilkul alag behave karte hain. Pehli write ke baad writer ko apni private copy mil jaati hai, doosre ko purani saaf copy. Aur read-only marking dono (parent aur child) pe lagti hai — jo pehle likhega, usi ko fault aur copy milti hai. Formula yaad rakho: Tcow=Nct+WccT_{cow} = N c_t + W c_c — agar likhe gaye pages WW bahut kam hain, to COW jeet jaata hai.

Test yourself — Operating Systems

Connections