📝 Copy on Write in virtual Memory (12 MCQs)
Deferred Memory Duplication Through Shared Pages
Copy-on-write is a virtual-memory optimisation that allows multiple processes to share the same physical memory pages until one attempts to modify them. The operating system initially maps the pages as shared and protects them against writing. When a process performs a write, a protection fault triggers creation of a private copy. This approach reduces unnecessary memory duplication during process creation and other sharing operations.
Why Immediate Process Memory Copying Is Expensive
Creating a complete physical copy of a process address space can consume substantial memory and processing time, even when the new process immediately replaces its program image. Copy-on-write avoids copying unchanged pages. It is particularly useful with Unix-style fork operations, where parent and child initially share memory but may subsequently modify different portions of their address spaces.
Read-Only Sharing and Write-Fault Handling
The kernel establishes shared mappings and marks eligible pages read-only or copy-on-write in the relevant page tables. Both processes can read the common frame. When a process writes, the MMU generates a protection fault. The kernel allocates a frame, copies the original contents, updates the writer's mapping, and permits the modification to continue.
Forking a Process with Shared Memory Pages
Suppose a parent process occupies 100 memory pages and creates a child using fork. Initially, the processes share the existing physical pages instead of immediately creating another 100 copies. When the child modifies page 12, the operating system creates a private copy for that page. The remaining unchanged pages can continue sharing their original frames.
Memory Savings and First-Write Costs
Copy-on-write reduces memory consumption and accelerates process creation when many pages remain unchanged. It also supports efficient private mappings of file-backed data. However, the first write to a protected shared page introduces a fault and copying overhead. Multiple writers can eventually require separate frames, and incorrect reference-count or permission management could compromise isolation.
Clarifying Shared Pages and Copy Behaviour
'Copy-on-write immediately duplicates every shared page' is incorrect because copying occurs only when a qualifying write requires it. 'Both processes always modify the same physical copy' is incorrect because a writer receives a private frame. 'Every protection fault indicates an illegal operation' is incorrect because a copy-on-write fault is an expected event handled by the kernel.
Exam Focus: Fork Operations and First-Write Faults
Trace how parent and child processes initially share physical frames and identify when a private copy becomes necessary. Calculate memory savings when only a specified number of pages are modified. Distinguish copy-on-write faults from invalid-address faults and ordinary page replacement. Explain why page-table permissions and frame reference counts help maintain correct sharing.
Key Terms of Copy-on-Write
- COW - Copies memory when modified.
- Shared frame - Physical frame used by processes.
- Fork - Creates a child process.
- Protection fault - Triggers a restricted-access exception.
- Reference count - Tracks mappings to a frame.
- Private copy - Independently writable page.
📝 All Copy on Write in virtual Memory MCQs
Q1. What does the term copy‑on‑write (COW) describe in virtual memory management?
📖 Explanation: Copy‑on‑write means that when two processes reference the same physical page, the OS lets them share it read‑only. Only when one process attempts to modify the page does the kernel allocate a new physical page and copy the original contents, giving the writer a private copy. Options A and D describe eager duplication, which COW explicitly avoids. Option C is unrelated because COW works for writable pages, not just read‑only mappings. This behavior saves memory until a write is needed.
Q2. Why are sparse address spaces advantageous when loading shared libraries?
📖 Explanation: Sparse address spaces contain large unmapped holes between allocated regions such as the heap and stack. Those holes occupy virtual addresses but no physical memory, so the system only allocates pages when a process actually grows into the hole. This saves RAM and reduces paging overhead. Option A is false because page tables are still required. Option C is unrelated; collisions are prevented by separate growth directions, not by sparsity. Option D is incorrect because libraries can be loaded at different addresses using relocation.
Q3. Process A calls fork(), creating Process B. Both have a read‑only mapping of libX.so. Immediately after fork, Process B writes to a global variable in libX. Which mechanism handles the write?
📖 Explanation: When Process B attempts to modify a read‑only page, the CPU raises a protection fault. The kernel then applies copy‑on‑write: it allocates a new physical page, copies the original contents, and maps the new page as writable for Process B. This leaves Process A still sharing the original page. Option A describes demand paging, which brings pages into memory but does not duplicate them on write. Option C would be unnecessary overhead, and Option D would violate program semantics.
Q4. How does copy‑on‑write complement demand paging in a system that uses lazy loading of pages?
📖 Explanation: Demand paging brings a page from disk into RAM the first time any process accesses it, regardless of read or write. Copy‑on‑write adds a second layer: after a page is present, it remains shared read‑only until a process writes to it, at which point the kernel copies the page. Thus demand paging handles initial loading, and COW reduces unnecessary copying for read‑only uses. Options A and C misrepresent the relationship, and D is inaccurate because COW works for both file‑backed and anonymous pages.
Q5. Three processes concurrently load the same large shared library. Without copy‑on‑write, each would have separate physical copies. Which outcome results from using copy‑on‑write for the read‑only mappings?
📖 Explanation: With copy‑on‑write, the kernel maps the library read‑only for each process, allowing them to share the same physical pages. Only when a process attempts to modify a page does the kernel create a private copy for that process. Therefore memory is conserved while preserving correctness. Option A describes the situation without COW. Option C is false because all processes can map the library. Option D mischaracterizes swapping, which is unrelated to the sharing mechanism.
Q6. A process has a read‑only shared mapping of a configuration file. It attempts to modify a setting, causing a page fault. What does the OS do?
📖 Explanation: When the process writes to a read‑only page, the hardware raises a protection fault. The kernel's copy‑on‑write handler allocates a new physical page, copies the original data, and remaps the page as writable for the faulting process. This preserves the original shared view for other processes. Option A would only occur if the mapping were not COW-enabled. Option C would break program semantics, and Option D would be far more expensive than a single‑page copy.
Q7. What is a primary trade‑off when using copy‑on‑write for inter‑process shared memory versus using explicit shared memory segments?
📖 Explanation: Copy‑on‑write saves memory while pages remain read‑only, but the first write to a shared page triggers a copy, introducing latency and extra memory use. Explicit shared memory segments are set up for concurrent read/write access, avoiding the copy cost but requiring synchronization mechanisms and potentially more memory if many processes write simultaneously. Option A is opposite of the usual benefit. Option C is false because shared memory can have protection flags. Option D is incorrect; COW works perfectly with fork.
Q8. A process’s virtual address space has a hole from to . The stack grows downwards and currently ends at . If the stack expands by bytes, which address will cause the first page fault?
📖 Explanation: The stack top is at and grows toward lower addresses. Expanding by bytes moves the stack down to . The first address that enters the unmapped hole is the boundary at ; when the stack crosses this address, the page is not present, causing a fault. Options B and D are addresses inside the already‑mapped region, and C is the original top, which is already valid.
Q9. A database system implements point‑in‑time snapshots using copy‑on‑write. At time T0, a process forks to create a child that will modify data. Which sequence correctly describes memory handling to keep the snapshot intact?
📖 Explanation: At T0 the parent and child share all pages read‑only. When the child writes to a page, copy‑on‑write triggers a copy of that specific page, preserving the original for the parent’s snapshot. This selective copying minimizes overhead while maintaining a consistent view. Option B would waste memory by copying everything. Option C would break the snapshot because the parent would lose its data. Option D ignores the in‑memory snapshot mechanism.
Q10. In workloads with frequent writes to shared library data, how does copy‑on‑write affect overall system performance compared to pre‑allocating private copies?
📖 Explanation: When writes are rare, copy‑on‑write saves memory and reduces copying. However, with a high rate of writes, each write may trigger a page copy, leading to many page faults and extra memory traffic, which can be slower than having each process start with its own private copy. Option A is too absolute; Option C is false because page faults still occur on writes. Option D is nonsensical.
Q11. Process X creates a child via fork. Without copy‑on‑write, each would need 200 MB of physical memory for its address space. With copy‑on‑write, only 30 MB of pages are written before the child exits. Approximate memory saved?
📖 Explanation: Initially both processes share the same 200 MB of pages read‑only, consuming only 200 MB total. During execution, 30 MB of pages are copied because of writes, adding 30 MB of private memory. The total physical memory used becomes 200 MB + 30 MB = 230 MB, but without COW it would have been 400 MB. The saved memory is roughly 400 MB − 230 MB = 170 MB. Options B and C misinterpret the calculation, and D suggests no saving, which is incorrect.
Q12. What security implication arises from copy‑on‑write pages that are initially read‑only shared among processes?
📖 Explanation: Because pages are shared read‑only until a write occurs, a malicious process cannot directly alter another process’s memory, but timing attacks can reveal whether a page is still shared or has been copied, giving hints about other processes’ activity. Option A is false because writes trigger a private copy. Option B misstates the read capability. Option D is unrelated; COW does not provide encryption.