🎓 BookMCQ
← Back to 9. Virtual Memory

📝 Shared memory in the Windows API (18 MCQs)

Interprocess Communication Through Windows Shared Mappings

Shared memory in the Windows API allows multiple processes to access a common memory region for interprocess communication. Windows provides file-mapping objects that can be backed by a regular file or by the paging file. Processes create or open a mapping object and map views into their virtual address spaces. Shared physical pages allow information exchange without repeatedly copying complete messages between processes.

Why Processes Use a Common Memory Region

Processes normally operate in separate virtual address spaces to preserve isolation. Communication through repeated file or message transfers can introduce copying and system-call overhead. Shared memory provides a common region that participating processes can access directly through mapped views. However, sharing introduces coordination requirements because simultaneous writes can produce inconsistent information without appropriate synchronization.

Creating Mapping Objects and Opening Mapped Views

A process uses CreateFileMapping to create or obtain a file-mapping object and MapViewOfFile to map a region into its address space. Another process can open the named mapping object using OpenFileMapping and map its own view. When communication ends, processes unmap views using UnmapViewOfFile and release their mapping-object handles.

Exchanging Data Through a Shared Mapping

Suppose two processes map the same 4096-byte paging-file-backed region. Process A writes a message into a designated shared-memory area, and process B reads that area through its own mapped view. Their virtual addresses may differ, but the views refer to shared physical contents. Synchronization ensures B reads a complete message rather than partially updated data.

Communication Speed and Synchronization Requirements

Shared mappings can provide efficient communication because processes access common memory directly rather than repeatedly transferring copied messages. Their performance depends on memory access patterns, synchronization design, and cache coherence. Windows applications may coordinate access through mutexes, events, or other synchronization objects. Incorrect coordination can cause race conditions, stale observations, or data corruption despite the shared mapping itself being valid.

Common Errors About Windows Shared Memory

'MapViewOfFile automatically synchronizes processes' is incorrect because mapping does not provide mutual exclusion. 'Both processes must receive identical virtual addresses' is incorrect because mapped views can have different virtual locations. 'Every file-mapping object must represent an ordinary disk file' is incorrect because a mapping can also use the paging file as its backing store.

Exam Focus: Windows Mapping API and Synchronization

Identify the roles of CreateFileMapping, OpenFileMapping, MapViewOfFile, and UnmapViewOfFile. Trace how two processes access the same mapped region using different virtual addresses. Distinguish shared-memory mapping from synchronization and explain why mutexes or events may be necessary. Recognise the difference between file-backed and paging-file-backed mapping objects.

Key Terms of Windows Shared Memory

  • File-mapping object - Represents a shared mapping.
  • CreateFileMapping - Creates a mapping object.
  • MapViewOfFile - Maps a view into memory.
  • OpenFileMapping - Opens an existing mapping object.
  • Mutex - Provides mutual exclusion.
  • UnmapViewOfFile - Removes a mapped view.
4
Easy
9
Medium
5
Hard

📝 All Shared memory in the Windows API MCQs

Q1. What does the copy‑on‑write mechanism do when a child process writes to a shared page?

A.It changes the original page for both processes
B.It creates a private copy for the child while leaving the parent’s page unchanged ✅
C.It deletes the page from both processes
D.It merges the child’s changes back into the parent’s page
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The mechanism creates a separate physical copy for the child, so the parent continues to use the original page unchanged. This isolates the child’s modifications, preventing unintended side effects. The other options describe actions that never happen: the original page is not altered, removed, or merged back. Understanding this behavior explains why separate copies are only made for pages that are actually written to.

Q2. Which operating system feature is commonly used to provide a pool of free pages for copy‑on‑write operations?

A.Dynamic memory allocation
B.Zero‑fill‑on‑demand
C.Thread‑local storage ✅
D.Interrupt handling
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Zero‑fill‑on‑demand prepares blank pages in advance, allowing the kernel to satisfy copy‑on‑write requests quickly. Dynamic allocation occurs at run time but does not guarantee pre‑zeroed pages. Thread‑local storage and interrupt handling are unrelated to page provisioning. Recognizing the role of zero‑fill‑on‑demand helps explain how the system can allocate a clean page without copying existing data.

Q3. Why are pages that contain executable code typically not marked as copy‑on‑write?

A.Because they are read‑only and never need duplication
B.Because they are too large to copy
C.Because the CPU cannot handle copy‑on‑write for code
D.Because they are stored in a special cache ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: Executable code pages are usually mapped as read‑only, so they never need to be written to, eliminating the need for copy‑on‑write. Their immutability allows multiple processes to share the same physical memory safely. The other answers suggest size or hardware constraints that are not the primary reason; the key factor is the read‑only nature of code pages.

Q4. What primary benefit does sharing unmodified pages between a parent and its children provide?

A.Faster CPU execution
B.Reduced physical memory consumption ✅
C.Improved network latency
D.Higher disk I/O throughput
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: When pages are shared, only one physical copy exists, so the total memory footprint is smaller. This conserves RAM and can reduce paging activity. Faster execution, network latency, and disk throughput are indirect effects, but the most direct advantage is the reduction in memory usage, which is why operating systems strive to share unchanged pages.

Q5. If a process attempts to write to a page marked copy‑on‑write, which sequence correctly describes the kernel’s actions?

A.Allocate a new page, copy the contents, update the page table, then perform the write ✅
B.Immediately write to the original page
C.Swap the entire process to disk
D.Raise an access‑violation exception
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The kernel first allocates a fresh physical page, copies the original data onto it, updates the process’s page‑table entry to point to the new page, and finally allows the write. This preserves the parent’s view while giving the child its own writable copy. Directly writing to the original page would break isolation, swapping the whole process is unnecessary, and an exception is only raised for illegal accesses, not for legitimate copy‑on‑write writes.

Q6. Two processes each allocate a new 4 KB heap page after a fork. If pages A and B are shared and only page C is copied‑on‑write, how many physical pages are used in total?

A.2
B.3 ✅
C.4
D.5
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Initially, pages A and B are shared, counting as one physical page each. When the child modifies page C, a second physical copy of C is created, so the total becomes three pages: A, B, and the new copy of C. The other options either ignore the duplicated C or add extra pages that are not required.

Q7. How does zero‑fill‑on‑demand contribute to system security?

A.It encrypts all allocated pages ✅
B.It guarantees that newly allocated pages contain no residual user data
C.It blocks all write accesses to new pages
D.It forces processes to use only read‑only memory
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: By zeroing pages before they are handed to a process, any leftover data from previous owners is erased, preventing accidental or malicious exposure of sensitive information. Encryption is not performed by this mechanism, and it does not block writes or enforce read‑only status. The security benefit lies in the clean slate each allocation provides.

Q8. A server creates many child processes that only need to read a large shared library. Which memory‑management technique minimizes RAM usage?

A.Copy‑on‑write for the library pages
B.Private copies for each child
C.Page‑level encryption
D.Dynamic recompilation of the library ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Using shared memory (e.g., a memory‑mapped file) lets all children map the same physical pages of the library, avoiding duplication. Copy‑on‑write would still create copies if any child attempted a write, which is unnecessary for read‑only access. Encryption and recompilation do not affect sharing and would add overhead.

Q9. What is the purpose of the free‑page pool when handling copy‑on‑write faults?

A.To store temporary files on disk
B.To hold pre‑zeroed pages ready for immediate use ✅
C.To queue processes waiting for CPU time
D.To cache network packets
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The pool contains pages that have already been zeroed, so when a copy‑on‑write fault occurs, the kernel can quickly allocate a clean page without waiting for disk I/O. Storing temporary files, queuing CPU time, or caching packets are unrelated responsibilities.

Q10. After a fork, Task Manager shows the memory usage of the parent process roughly doubling even though no extra work was done. Which explanation is most plausible?

A.The child process caused copy‑on‑write of many pages ✅
B.The operating system duplicated all pages regardless of modification
C.The parent process allocated additional heap space
D.Task Manager incorrectly sums shared memory twice
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: When the child writes to many pages, each write triggers a copy‑on‑write, creating separate physical copies and increasing the reported usage. The OS does not duplicate every page indiscriminately, and the parent did not allocate extra heap. Modern Task Managers usually account for shared pages, so double counting is unlikely.

Q11. Which Windows API function is commonly used to create a named shared memory region between processes?

A.CreateProcess
B.CreateFileMapping ✅
C.VirtualAllocEx
D.ReadFile
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: CreateFileMapping can create a memory‑mapped file or a page file backed region that multiple processes can open by name, enabling shared memory. CreateProcess starts a new process, VirtualAllocEx allocates memory in another process without sharing, and ReadFile performs I/O on files, not memory sharing.

Q12. A system has 4 GB of RAM. Process 1 owns pages A, B, and C (each 4 KB). After a child modifies page C using copy‑on‑write, how much physical memory do both processes occupy for these three pages?

A.8 KB
B.12 KB
C.16 KB ✅
D.20 KB
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Pages A and B remain shared, using 8 KB total. Page C is duplicated, adding another 4 KB for the child’s copy. Thus the combined physical usage is 12 KB. The other answers either double‑count shared pages or add extra pages that were not created.

Q13. Which statement correctly highlights a key difference between Windows and Linux copy‑on‑write implementations?

A.Linux always uses a global zero‑fill pool, while Windows does not
B.Windows can mark executable pages as copy‑on‑write, but Linux cannot ✅
C.Linux tracks reference counts at the page‑table level, whereas Windows tracks them in a separate structure
D.Both systems use identical mechanisms with no differences
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Windows permits marking executable pages as copy‑on‑write, allowing them to be duplicated if a write occurs, while Linux typically treats executable pages as read‑only and does not mark them copy‑on‑write. The other options misstate how reference counting or zero‑fill pools work; both OSes maintain reference counts, and both use zero‑fill techniques.

Q14. If zero‑fill‑on‑demand fails and returns a page containing leftover data, what risk does this introduce?

A.Increased CPU utilization ✅
B.Potential leakage of confidential information
C.Higher network latency
D.Improved application performance
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: When a page isn’t properly zeroed, residual data from previous allocations may be exposed to a new process, creating a confidentiality breach. The primary risk is data leakage, not CPU usage, latency, or performance gains. Therefore, the correct concern is the possibility of exposing sensitive information.

Q15. Why is copy‑on‑write considered a lazy allocation technique?

A.It allocates memory only after a write occurs
B.It pre‑allocates all possible pages at process start
C.It immediately copies all pages during fork ✅
D.It continuously swaps pages to disk
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The technique postpones the actual duplication of pages until a process attempts to modify them, thereby avoiding unnecessary copying. Pre‑allocation, immediate copying, or constant swapping would not be lazy; they would consume resources upfront. Recognizing this laziness explains why copy‑on‑write reduces memory overhead for many forked processes.

Q16. A program uses MapViewOfFile to share a memory‑mapped file. Under which condition will another process see the writer’s changes instantly?

A.When both processes have read‑only views
B.When the file is opened with FILE_FLAG_WRITE_THROUGH
C.When the mapping is created with PAGE_READWRITE and both processes map the same region ✅
D.When the writer flushes the view with FlushViewOfFile
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: If both processes map the same region with PAGE_READWRITE, modifications are made directly to the shared physical pages, so changes become visible immediately. Read‑only views cannot write, FILE_FLAG_WRITE_THROUGH concerns disk I/O, and flushing merely ensures data reaches the file, not that other processes see it instantly.

Q17. When a copy‑on‑write page is duplicated, which kernel data structures must be updated?

A.Process priority queues
B.Page‑table entries and reference‑count tables
C.Interrupt descriptor tables
D.User‑mode thread stacks ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: The kernel must adjust the page‑table entry for the faulting process to point to the new physical page and increment or decrement reference counts in the page‑frame database. Process priorities, interrupt tables, and user‑mode stacks are unrelated to the copy‑on‑write operation.

Q18. In a multi‑threaded server, each thread spawns a child process that inherits a large read‑only configuration table. Which setting minimizes memory duplication?

A.Mark the configuration pages as copy‑on‑write ✅
B.Allocate a private heap for each child
C.Use a memory‑mapped file with PAGE_READONLY
D.Disable virtual memory for the children
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Marking the read‑only configuration pages as copy‑on‑write (or simply read‑only) lets the OS share the same physical pages across all children until a write is attempted, which never happens. Using a private heap would duplicate memory, a memory‑mapped file with PAGE_READONLY also shares pages but requires extra API calls, and disabling virtual memory is impractical.

🔗 Related Topics (MCQs)