What is copy-on-write, and why is it useful?
Interview preparation resource from Gate Smashers.
Copy-on-write lets processes initially share the same physical pages as read-only and creates a private copy only when one process attempts to modify a shared page.
After fork(), duplicating every page immediately would consume time and memory even though the child often calls exec() soon afterwards. With copy-on-write, parent and child page tables point to the same physical frames, and the mappings are protected from writing.
A write causes a protection fault. The kernel allocates a new frame, copies that page, updates the writer's mapping and restarts the instruction. Pages that are never modified remain shared. The same idea supports efficient snapshots, zero pages and memory deduplication designs.
The optimization is especially valuable after fork() because the child often calls exec() before changing most inherited pages. Instead of copying the complete address space, the system copies only pages that are actually modified, reducing both launch time and temporary memory use.
