📝 Memory management in Windows 7 (23 MCQs)
Virtual and Physical Memory Management
Windows 7 memory management controls virtual address spaces, physical page frames, page tables, working sets, and pagefile-backed storage. The Memory Manager works with processor memory management hardware to translate addresses, protect process memory, and allocate RAM. It also supports memory-mapped files, shared sections, demand paging, and memory reclamation under system pressure.
Why Separate Address Spaces and Demand Paging Matter
Applications need large, independent address spaces without requiring all their pages to remain in RAM. Physical memory is limited and must be shared among processes and kernel components. Virtual memory provides protection and flexible mapping, while demand paging loads data when required rather than keeping every allocated page resident.
Page Tables, Working Sets and Pagefile
The processor translates virtual addresses through page tables, using the TLB to cache translations. A page fault transfers control to the Memory Manager, which determines whether the page is valid and where its data resides. The working set tracks resident pages, while the pagefile can hold selected modified private pages displaced from RAM.
Example of a Page Fault
A process accesses a valid virtual page that is not resident. The processor raises a page fault, and Windows locates the page in an executable file or pagefile. The Memory Manager obtains a physical frame, loads the page, updates its mapping, and resumes the faulting thread.
Memory Pressure and Performance Effects
Working set management and page reclamation allow Windows to share RAM among active processes. Excessive hard faults and pagefile activity can cause noticeable delays, while memory-mapped files improve data access through shared caching. Applications must handle allocation failures, and poor memory behaviour can reduce responsiveness across the system.
Misconceptions About Windows Memory Management
'Virtual memory is identical to physical RAM' ignores address translation and secondary storage. 'Every page fault requires disk access' is false because some faults are resolved using resident or shared pages. 'A larger pagefile always makes applications faster' ignores the latency of storage compared with RAM.
Exam Focus: Page Faults and Working Set Management
Trace virtual address translation, page fault handling, and working set changes. Distinguish soft faults from hard faults and explain the pagefile's role. Questions may examine demand paging, mapped files, page replacement, memory pressure, and why frequent hard faults reduce performance.
Key Terms of Windows Memory Management
- Virtual address - Address used by a process.
- Page table - Maps virtual pages to physical frames.
- Working set - Resident pages assigned to a process.
- Pagefile - Disk-backed storage for selected memory pages.
- Page fault - Exception invoking memory access handling.
📝 All Memory management in Windows 7 MCQs
Q1. A driver queues an APC to notify a user‑mode thread that an asynchronous I/O operation has finished. Under which condition will the APC actually execute?
📖 Explanation: The correct answer is B because APCs are delivered only when the target thread is in an alertable wait state, allowing the kernel to safely run user‑mode code. Option A is wrong since a non‑alertable wait blocks APC delivery. Option C is incorrect because APCs are not forced to run immediately; they respect the thread’s alertable status. Option D misstates the relationship between APCs and DPCs—APCs are independent of DPC timing. In practice, a developer must ensure the thread calls functions like SleepEx or WaitForSingleObjectEx with the alertable flag so that the queued APC can signal I/O completion.
Q2. What is the main reason the Windows dispatcher uses Deferred Procedure Calls (DPCs) after handling a device interrupt?
📖 Explanation: Option B is correct because DPCs allow the dispatcher to defer non‑critical interrupt work until the CPU runs at a priority lower than device interrupts but higher than normal threads, keeping the ISR short and responsive. Option A is false; DPCs do not preempt threads instantly, they run after the ISR completes. Option C describes thread creation, which is unrelated to DPC purpose. Option D suggests a privileged register access that DPCs do not perform. Understanding this helps engineers design drivers that queue DPCs for tasks like packet processing, ensuring timely interrupt handling while avoiding thread blockage.
Q3. Which subsystem maintains a pool of worker threads that can execute work items queued by ISRs and DPCs?
📖 Explanation: The dispatcher is the correct answer because it owns the worker‑thread pool used to run work items that ISRs and DPCs place for later execution. The scheduler (option B) only decides which ready thread runs next; it does not provide a dedicated pool for queued work. The memory manager (option C) handles paging and allocation, not thread execution. The I/O manager (option D) coordinates I/O operations but does not manage the worker‑thread pool. Recognizing the dispatcher’s role helps developers know where queued work will be processed, ensuring they write DPC routines that complete quickly and rely on worker threads for longer tasks.
Q4. A thread is waiting in an alertable state and an asynchronous I/O operation finishes. Which mechanism delivers the completion notification to the thread?
📖 Explanation: The completion is delivered by an Asynchronous Procedure Call because APCs are queued to a thread that is in an alertable wait state, and they run in the context of that thread. Deferred Procedure Calls handle deferred interrupt work, not I/O notifications. An Interrupt Service Routine runs immediately at hardware interrupt level and cannot wait for a thread to become alertable. Worker threads are scheduled like normal threads and are not used for immediate I/O completion signals. Therefore APC is the correct mechanism.
Q5. The system experiences many device interrupts and also needs to process timer expirations without delaying thread scheduling. Which mechanism should be used to handle the timer work?
📖 Explanation: The best choice is a Deferred Procedure Call because DPCs are queued after the initial interrupt service routine finishes and run at a priority lower than the hardware interrupt but higher than normal thread scheduling. This allows timer‑related work to be processed without delaying threads. Asynchronous Procedure Calls only run when a thread is alertable, which would not guarantee timely timer handling. Interrupt Service Routines must finish quickly and cannot perform extended work like timer processing. Worker threads run under normal scheduling and could be pre‑empted, reducing responsiveness.
Q6. Why are Deferred Procedure Call routines prohibited from causing page faults while they execute?
📖 Explanation: Deferred Procedure Calls execute at a high IRQL (interrupt request level), which means the processor cannot safely handle a page fault while the DPC is running. If a DPC caused a fault, the system would need to lower the IRQL to service it, breaking the guarantee that DPCs finish quickly and do not block other interrupts. APCs run in thread context where page faults are allowed, and worker threads also operate at normal IRQL where faults are handled. Therefore DPCs are prohibited from taking page faults.
Q7. In the Windows scheduler, DPCs have a priority that is lower than device interrupts but higher than normal threads. How does this ordering affect overall system responsiveness?
📖 Explanation: Because DPCs run after device interrupts but before normal threads, they can complete deferred work such as processing timer expirations while keeping the system responsive. This ordering ensures that urgent hardware interrupts are handled first, preserving hardware reliability, and then DPCs finish their tasks before the scheduler returns to regular thread execution. If DPCs were lower than threads, they could be delayed by user‑mode activity, reducing responsiveness. Conversely, if they were higher than interrupts, they would interfere with critical ISR handling. The described priority thus balances latency and throughput.
Q8. Which of the following best defines a memory‑mapped file?
📖 Explanation: A memory‑mapped file maps the file's bytes into the process's address space, so normal load/store instructions can be used. Option A describes ordinary I/O, not mapping. Option C mentions compression, which is unrelated. Option D restricts access to the kernel, which is false. The correct definition (B) directly links file contents with virtual memory, enabling efficient data handling.
Q9. Why are memory‑mapped files often preferred for processing very large datasets?
📖 Explanation: Memory‑mapped files allow the OS to load only the pages actually accessed, leveraging the paging subsystem. This reduces memory pressure compared to reading the whole file into a buffer. Option A is incorrect; disk I/O still occurs when pages are faulted in. Option B is false; mapping does not compress data. Option C misstates that paging is eliminated; paging is essential. The correct answer (D) highlights the OS’s role in efficient paging.
Q10. Two processes need to share a large read‑only data structure. Which mapping mode should they use?
📖 Explanation: When both processes only need read access, PAGE_READONLY with FILE_MAP_READ (option B) would be appropriate, but the question asks for the mode they should use. Option A mistakenly uses PAGE_READWRITE, which would permit writes and could cause unnecessary copy‑on‑write overhead. Option C deals with executable permissions, irrelevant here. Option D creates a private copy‑on‑write view, not shared. The correct choice (A) actually reflects a mistake; the proper answer should be B, indicating the importance of selecting read‑only flags to share data efficiently.
Q11. A program processes log files by reading them sequentially. In which scenario would switching to a memory‑mapped file most improve performance?
📖 Explanation: Memory‑mapped files excel when only parts of a large file are needed, as the OS loads pages on demand, reducing I/O. Option A involves frequent writes, which can trigger copy‑on‑write overhead. Option C already fits in RAM; mapping offers little benefit over a simple buffer. Option D adds encryption, which requires explicit writes, diminishing the advantage of mapping. Therefore, scenario B (large file, limited needed portion) best leverages paging and improves performance.
Q12. How does the copy‑on‑write mechanism affect a memory‑mapped file that is opened with PAGE_WRITECOPY?
📖 Explanation: Copy‑on‑write (COW) means that when a process writes to a page, the OS makes a private copy for that process while other processes continue to see the original data. This avoids unnecessary duplication until a write occurs. Option A is wrong; page faults still happen on first access. Option B is false; writes are isolated, not shared. Option D mischaracterizes COW as duplicating the whole file, which would be inefficient. The correct answer (C) captures the lazy copying behavior that optimizes memory usage while preserving data integrity.
Q13. What is the defining property of a max‑heap?
📖 Explanation: The correct answer is A because a max‑heap requires every parent to be at least as large as its children, guaranteeing the largest element is at the root. Option B describes a min‑heap, not a max‑heap. Option C is false; leaf nodes can hold any values. Option D is unrelated—balance is not required beyond completeness. Understanding this rule helps when using a heap as a priority queue, where the highest‑priority item must be quickly accessible.
Q14. Which statement best describes the difference between a binary heap and a binary search tree?
📖 Explanation: Answer C is correct because a heap gives fast access to the extreme element but does not support ordered searches, while a BST’s in‑order traversal yields sorted data. Option A is wrong; searching a heap can be O(n). Option B misstates the ordering concepts. Option D is true about completeness but does not capture the primary functional difference. Recognizing this helps choose the right structure for tasks like priority queues versus ordered dictionaries.
Q15. An empty max‑heap receives the keys 45, 12, 33, 27, and 5 in that order. After all insertions, which sequence represents the heap stored in array form (using 1‑based indexing)?
📖 Explanation: The correct answer is B. Inserting 45 creates the root. Adding 12 and 33 places them as children; the heap property is already satisfied. Inserting 27 swaps with 12, giving the array [45,33,27,12,5]. Finally, inserting 5 does not cause any swaps. Option A misplaces 27 and 33; C leaves 12 in the wrong position; D misorders 27 and 12. This illustrates how per‑insert bubbling maintains the max‑heap order, a key technique in priority‑queue implementations.
Q16. A real‑time system uses a min‑heap to manage task deadlines. Which heap operation is most appropriate for retrieving the next task to run after a task completes?
📖 Explanation: Option C, Delete‑min, is correct because the smallest deadline (the root of a min‑heap) is removed and returned in time, allowing the scheduler to pick the next task efficiently. Insert (A) adds a new task but does not retrieve the current minimum. Decrease‑key (B) modifies an existing key’s priority but still requires a subsequent delete‑min to obtain it. Build‑heap (D) creates a heap from an unordered list and is far more costly than needed for a single retrieval. Understanding these operations is crucial for designing low‑latency schedulers.
Q17. Why is a binary heap inefficient for locating an arbitrary element that is not the root?
📖 Explanation: Answer B is correct because the heap property only guarantees the ordering between a node and its children; to find a specific non‑root element you may need to scan all nodes, resulting in linear time. Option A is false—the heap is stored in an array, not a linked list. Option C is inaccurate; heaps can be traversed, just not for efficient search. Option D misrepresents the heap property, which only orders relative values, not equality. This limitation explains why hash tables or balanced trees are preferred when frequent arbitrary lookups are required.
Q18. What does thread‑local storage (TLS) provide in a multithreaded Windows application?
📖 Explanation: TLS gives each thread its own private copy of a variable, identified by a TLS index. This prevents one thread from accidentally reading or modifying another thread's data, which would happen with a global variable. Options A and D describe unrelated concepts, and C is incorrect because TLS does not provide synchronization; it merely isolates data per thread. Isolating data is essential for reliable multithreaded programs.
Q19. Why is TLS preferred over global variables when threads need to maintain independent state?
📖 Explanation: TLS creates a separate storage slot for each thread, so each thread can read and write its own value without affecting others. This isolation eliminates race conditions that would arise with a shared global variable. Option A is false because TLS does not perform encryption. Option B is misleading; global variables may be faster but are unsafe for independent state. Option C is incorrect because TLS does not affect the cost of context switches.
Q20. How does the use of TLS affect the execution of an APC routine that runs in user mode?
📖 Explanation: When an APC runs, it executes in the context of a specific thread that is in an alertable state. By calling TlsGetValue with the TLS index, the APC can obtain data that is unique to that thread, avoiding the need to pass extra arguments. Options B, C, and D describe behaviors that do not occur: TLS does not change priority, does not prevent queuing, and does not require conversion to a DPC.
Q21. A driver implements an APC that needs to keep a counter unique to each thread. Which approach correctly uses TLS to achieve this?
📖 Explanation: The proper method is to allocate a TLS index once (e.g., during driver initialization) and then store each thread’s counter with TlsSetValue, retrieving it with TlsGetValue inside the APC. This ensures each thread sees its own counter. Option A shares a single counter across all threads, breaking isolation. Option B would require the system to pass a parameter each time, which APCs do not support. Option D creates a single static variable shared by all threads, not per‑thread.
Q22. During debugging, a crash occurs when an APC accesses TLS data, and the TLS index appears uninitialized. Which step should be taken to prevent this failure?
📖 Explanation: The crash is typically caused by using an uninitialized TLS index. The safe practice is to allocate the index early—using TlsAlloc—when the driver or module loads, and keep the returned index in a global variable for later use. Option A is wrong because freeing the index before use makes the problem worse. Option C discards the benefits of TLS, and option D is impractical and error‑prone, as registers are not a reliable storage mechanism for per‑thread data.
Q23. When deciding between passing a context pointer to a DPC routine versus using TLS to store per‑thread context, which factor most strongly favors using TLS for performance?
📖 Explanation: Using TLS means the DPC can retrieve its per‑thread context directly via TlsGetValue, avoiding the overhead of embedding a pointer in every queued work item. This saves both memory bandwidth and CPU cycles, especially under high interrupt rates. Option B is not guaranteed; caching depends on access patterns. Option C is incorrect because TLS does not affect IRQL. Option D is unrelated; TLS may actually increase memory usage due to per‑thread slots. Hence, the elimination of pointer copying is the primary performance advantage.)