📝 Basic mechanism of Memory Mapped Files (21 MCQs)
Accessing File Contents Through Virtual Memory
Memory-mapped files connect a file or part of a file to a process's virtual address space. Instead of repeatedly transferring data through conventional read and write operations, an application accesses mapped contents using ordinary memory references. The operating system manages the association between file offsets, virtual pages, and physical frames, while page faults bring required file data into memory as needed.
Why Mapping Files Simplifies Data Access
Traditional file access often requires explicit system calls and copying data between kernel-managed buffers and application memory. Memory mapping provides a convenient virtual-address interface and can reduce unnecessary copying. It also supports shared file mappings through which multiple processes access common file-backed pages. However, mapping does not mean the entire file must remain resident in physical memory.
Creating a Mapping and Handling Page Faults
The process requests a file mapping using the operating system's memory-mapping facilities and specifies an offset and length. The kernel establishes virtual-memory mappings backed by the file. When the process accesses an unmapped resident page, a page fault triggers retrieval of the relevant file data. Modified shared pages are eventually written back according to operating system policies.
Reading a File Through a Mapped Region
Suppose an application maps a 12 KB section of a file beginning at file offset 4096. With 4 KB pages, accessing the first mapped byte references the file's page beginning at offset 4096. Accessing a later page may trigger another fault. The application can read the mapped bytes through ordinary memory instructions without explicitly reading each block.
File Sharing, Dirty Pages and Mapping Costs
Memory mapping supports efficient file access, shared memory between processes, and access to large datasets without loading entire files eagerly. Shared mappings can expose modifications to other processes and eventually update the underlying file. Private mappings generally provide copy-on-write behaviour instead. Page faults, alignment restrictions, synchronization, file truncation, and dirty-page write-back remain important implementation considerations.
Distinguishing Shared and Private File Mappings
'Mapping a file loads its entire contents immediately' is incorrect because pages can be loaded on demand. 'Every modification to a private mapping updates the original file' is incorrect because private mappings generally use copy-on-write semantics. 'A mapped file requires no page-fault handling' is incorrect because nonresident file-backed pages still need memory-management intervention.
Exam Focus: File Offsets and Shared Mapping Behaviour
Trace how a file offset corresponds to a mapped virtual page and identify when a page fault occurs. Compare shared and private mappings, including write propagation and copy-on-write. Explain why page-aligned offsets are commonly required by mapping interfaces. Questions may also examine how several processes can access common file-backed physical pages.
Key Terms of Memory-Mapped Files
- Memory mapping - Associates files with virtual memory.
- File offset - Position within a file.
- Shared mapping - Allows shared mapped updates.
- Private mapping - Uses private modification semantics.
- Dirty page - Modified mapped page.
- Page fault - Loads unavailable mapped contents.
📝 All Basic mechanism of Memory Mapped Files MCQs
Q1. What does the copy-on-write (COW) technique primarily aim to reduce when a process forks?
📖 Explanation: Copy‑on‑write creates a shared physical page until one process writes to it, at which point a separate copy is made. This sharing avoids allocating a full set of duplicate pages, directly reducing the amount of memory consumed. Options B, C, and D describe unrelated performance aspects; they are not the primary goal of COW. By saving memory, COW lets more processes run concurrently on the same hardware.
Q2. Which term describes pages that are cleared to zero before being handed to a process?
📖 Explanation: Zero-fill-on-demand pages are pre‑cleared to all zeros before allocation, ensuring no leftover data from previous owners remains. This protects security and provides a clean starting state. The other terms refer to different mechanisms: lazy allocation delays mapping, dirty pages have been modified, and copy‑on‑write deals with sharing until a write occurs. Hence, zero‑fill-on-demand is the correct description.
Q3. Why are pages that contain executable code typically not marked as copy‑on‑write?
📖 Explanation: Executable code pages are loaded as read‑only, so neither parent nor child will attempt to modify them. Since no write occurs, there is no need to create a private copy, and the pages can remain shared safely. The other options are inaccurate: size does not prevent copying, the OS can track execution, and page faults happen only on writes, not reads.
Q4. When a child process writes to a stack page that was marked copy‑on‑write, what happens to the original parent page?
📖 Explanation: The parent’s page stays intact because the child’s write triggers a copy operation, leaving the original page untouched for the parent. Deletion would break the parent’s execution, swapping is unrelated to the write event, and the original page is already read‑only, so no change is needed. This behavior preserves isolation while still allowing sharing of unmodified pages.
Q5. A system has a pool of 5 free pages. Process 1 forks, and the child immediately writes to two distinct pages marked copy‑on‑write. How many free pages are consumed for the child’s copies?
📖 Explanation: Each write to a copy‑on‑write page forces the OS to allocate a new physical page for the child’s private copy. With two writes, two free pages are taken from the pool, leaving three still available. No additional pages are needed for the parent, and no pages are freed during the operation, so the correct count is two.
Q6. If a process modifies three pages after a fork, and the system initially shared all pages, how many total physical pages exist for those three after the modifications?
📖 Explanation: Initially, parent and child share three pages. When the child modifies each page, a separate copy is allocated for each, adding three new pages. The parent still holds the original three, so the total becomes six physical pages representing those three logical pages across both processes.
Q7. How does copy‑on‑write interact with a page‑replacement algorithm like LRU?
📖 Explanation: Copy‑on‑write itself does not change the criteria used by LRU; pages are still evicted based on recent use. However, because shared pages can serve two processes, they might be accessed more frequently, indirectly influencing LRU decisions. The other statements overstate the effect or claim actions that do not occur.
Q8. When the free‑page pool is exhausted and a copy‑on‑write fault occurs, what is the OS most likely to do?
📖 Explanation: If no free pages are immediately available, the OS typically triggers a page‑replacement operation to free a page, then fulfills the copy‑on‑write request. Delaying the fault until a page becomes free allows the process to continue without termination. Allocating from swap or ignoring the fault would either be unnecessary or break memory consistency.
Q9. In a system that uses both zero‑fill‑on‑demand and copy‑on‑write, what order of operations occurs when a child process receives a new page after a fork?
📖 Explanation: When a new page is needed for the child, the OS first obtains a zero‑filled page to ensure no residual data remains. After the page is assigned, if the page can be written, it is marked copy‑on‑write so future writes trigger a private copy. This sequence guarantees both security (zero‑fill) and efficient sharing (COW).
Q10. Why are executable code pages typically shared between parent and child after a fork?
📖 Explanation: Executable sections are loaded as read‑only, meaning neither process will attempt to modify them. Because no write occurs, the OS can keep a single physical copy and map it into both address spaces, saving memory. The other options either mischaracterize the nature of code pages or describe unrelated behaviors.
Q11. A diagram shows process 1 with pages A, B, C and process 2 with a copy of page C after a fork. If the child modifies page C, which pages will now exist in physical memory?
📖 Explanation: When the child writes to its copy of page C, the OS allocates a new physical page for the child’s version, leaving the original page C for the parent. Consequently, physical memory contains pages A, B, the original C, and the newly created copy of C, totaling four distinct pages.
Q12. During a copy‑on‑write page fault, which sequence correctly describes the OS actions?
📖 Explanation: The OS first obtains a free physical page, copies the contents of the original shared page into it, updates the faulting process’s page table to point to the new page, and finally clears the copy‑on‑write attribute because the page is now private. Swapping is unnecessary, and termination would be incorrect.
Q13. How does vfork() differ from fork() with copy‑on‑write regarding memory usage?
📖 Explanation: vfork() creates a child that runs in the parent’s address space, postponing any copying until the child calls exec() or exits. This contrasts with fork() + COW, where pages are shared but may be copied on first write. Therefore, vfork() can use even less memory initially, while still providing a separate execution context.
Q14. If the parent writes to a page that the child has already copied via copy‑on‑write, what is the state of that page in physical memory?
📖 Explanation: After the child’s copy is created, the parent’s subsequent write triggers another allocation, producing a second private page. Thus, both processes now have separate physical pages for the same logical page. The child’s copy is not overwritten, sharing does not resume, and the original page remains allocated.
Q15. What security benefit does zero‑fill‑on‑demand provide when allocating a new page?
📖 Explanation: Zero‑fill‑on‑demand writes zeros to every byte of a newly allocated page, guaranteeing that no residual information from a prior process can be read by the new owner. This prevents accidental data leakage. Encryption and compression are separate techniques, and speed is not the primary security advantage.
Q16. A process forks and then writes to three distinct pages marked copy‑on‑write. How many total page copies will exist for those three pages after the writes?
📖 Explanation: Each write forces the OS to allocate a new private page for the child, resulting in three new pages. The parent still retains the original three pages, so the total number of physical pages representing those three logical pages becomes six.
Q17. Which operating systems are known to implement copy‑on‑write?
📖 Explanation: All of the listed systems—Windows XP, Linux, and Solaris—use copy‑on‑write as part of their memory‑management strategy. The other choices name platforms that either do not use COW in the same way or were not mentioned as examples.
Q18. Why is it important for the OS to allocate free pages for copy‑on‑write from a dedicated pool?
📖 Explanation: A dedicated pool of pre‑zeroed pages allows the OS to satisfy copy‑on‑write requests promptly, minimizing latency and preventing the need to search the entire memory, which could cause fragmentation. CPU usage and network I/O are unrelated, and page‑table integrity is maintained regardless of the source pool.
Q19. A system has four free pages. After a fork, the child modifies three pages that were copy‑on‑write. Can the OS satisfy the request without additional allocation?
📖 Explanation: The child needs a new physical page for each of the three modified pages, consuming three of the four free pages. Since enough free pages remain, the OS can allocate them directly without invoking page replacement or additional memory. Hence, the request is satisfied.
Q20. How does copy‑on‑write influence the behavior of a classic LRU page‑replacement algorithm?
📖 Explanation: When pages are shared via COW, each access by either process updates the page’s last‑used timestamp, potentially keeping the shared page higher in the LRU order. This indirect effect can change which pages are evicted, but LRU still considers all pages; it does not ignore shared pages nor is it disabled.
Q21. Design a simple algorithm to handle a copy‑on‑write fault using zero‑fill‑on‑demand. Which step is performed first?
📖 Explanation: The algorithm begins by pulling a zero‑filled page from the pre‑allocated pool, guaranteeing that no residual data is present. After allocation, the original contents are copied, the page‑table is updated, and finally the copy‑on‑write attribute is cleared. Starting with allocation ensures security and availability before any further actions.