Copy on Write needs page faults

· Source: Hussein Nasser · Field: Technology & Digital — Software Development & Engineering, Operating Systems · Depth: Intermediate, quick

Summary

Copy on Write (CoW) is a fundamental operating system mechanism that optimizes memory usage when processes are forked. Initially, a new child process created via forking receives a virtual memory address space nearly identical to its parent, but both processes share the same physical memory pages by sharing page tables. This avoids immediate, full memory duplication. A CoW page fault occurs when the child process attempts its first write to one of these shared memory pages. The kernel then intervenes, copying only that specific page to a new physical memory location, updating the child's virtual memory mapping to point to the new page, and finally allowing the write. This ensures process isolation, preventing one process from modifying another's data, while deferring memory copying until absolutely necessary.

Key takeaway

For software engineers optimizing multi-process applications, understanding Copy on Write (CoW) is crucial. Your application's performance during process forking is significantly impacted by how much data child processes modify. Minimize writes to shared memory in child processes to reduce CoW page faults, thereby improving memory efficiency and overall system responsiveness. Consider read-only data structures for shared information to fully benefit from CoW's deferred copying mechanism.

Key insights

Copy on Write (CoW) optimizes memory by sharing pages between forked processes, copying only upon a child's first write access via a page fault.

Principles

Method

When a child process attempts to write to a shared memory page, the kernel triggers a CoW page fault, copies the page to new physical memory, updates the child's virtual mapping, then permits the write.

In practice

Topics

Best for: Software Engineer, IT Professional

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Hussein Nasser.