Copy-on-write
WHAT is Copy-on-write?
The "unit" of copying in OS memory is the page (typically ). 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:
- Speed:
fork()becomes for table setup, not for data copy. - 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()):
- The child gets its own page tables, but every entry points to the same physical frames as the parent.
- All shared writable pages are marked read-only in both page tables, and flagged internally as COW.
- The OS keeps a reference count per physical frame: how many mappings share it.
- Reading? Fine — read-only allows reads. No fault, no copy.
- Writing to a COW page → CPU raises a page fault (write to read-only page).
- 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 , the remaining owner's page can be made writable again (no need to keep it COW).
- Re-execute the faulting instruction; now the write hits the private copy.

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?
What is the unit of copying in OS-level COW?
What hardware mechanism detects a COW write?
Why does fork() use COW?
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?
What is the COW time cost formula?
When is COW worse than eager copying?
What does the reference count track in COW?
After a COW page is copied and refcount becomes 1, what happens?
Does a process that only reads shared pages ever trigger a copy?
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
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: — agar likhe gaye pages bahut kam hain, to COW jeet jaata hai.