š Program structure in Virtual Memory (11 MCQs)
Program Organisation and Virtual Memory Locality
Program structure influences virtual-memory performance because code organisation and data-access patterns determine which pages are referenced during execution. Programs commonly exhibit temporal and spatial locality, accessing recently used data and nearby memory locations. The memory manager relies on these patterns when demand paging and page replacement operate. Efficient program organisation can therefore reduce page faults and improve the likelihood that useful pages remain resident.
Why Data Layout Determines Page Access Behaviour
Programs with scattered data references may touch many pages even when processing relatively little information. Sequential arrangements allow nearby values to share pages and reduce unnecessary memory transfers. A poorly organised access pattern can exceed the available working-set frames, especially in large arrays and multidimensional data structures. The underlying issue concerns the relationship between logical data layout and the physical pages accessed.
Sequential Execution and Working-Set Locality
The processor generates memory references as instructions execute and access their associated variables and structures. The MMU translates these references into page mappings. Sequential instruction execution often concentrates accesses within neighbouring code pages, while loops repeatedly access particular data regions. Demand paging loads missing pages, and replacement policies attempt to retain the active collection that supports the current program phase.
Comparing Row-Major and Column-Major Array Traversal
Consider a 100 by 100 integer array stored in row-major order with four-byte elements. Traversing each row consecutively accesses adjacent elements, using nearby memory locations. Traversing the array column by column jumps between distant rows and can touch more pages during a short interval. The row-wise pattern generally provides better spatial locality for this storage layout.
Loop Design, Page Locality and Fault Reduction
Organised data structures and cache-friendly loops can reduce the number of active pages and improve virtual-memory performance. Poor locality increases page working-set size and can cause repeated faults when available frames are insufficient. Changing traversal order, blocking large matrix operations, and grouping related objects can improve locality. Benefits depend on actual page layout, hardware caches, and application workload characteristics.
Common Misunderstandings About Program Locality
'Sequential instruction execution guarantees that all data references are nearby' is incorrect because operands may be scattered. 'A large array always causes thrashing' is incorrect because access locality and frame availability determine fault behaviour. 'Row-major and column-major traversal always have identical locality' is incorrect because storage layout determines which traversal accesses adjacent elements.
Exam Focus: Reference Patterns and Page Locality
Trace memory references in loops and identify the virtual pages accessed within a specified interval. Compare sequential and strided array traversal under a known page size. Determine how changing loop order or blocking affects working-set size. Explain the relationship between program structure, spatial locality, temporal locality, and the number of page faults.
Key Terms of Program Structure
- Locality - Concentration of memory references.
- Spatial locality - Access to nearby addresses.
- Temporal locality - Reuse of recent references.
- Working set - Recently active virtual pages.
- Row-major - Stores consecutive row elements.
- Blocking - Processes data in smaller regions.
š All Program structure in Virtual Memory MCQs
Q1. A system reserves a fixed 20āÆ% of its memory for I/O buffers. When a running process causes a page fault and the freeāframe list is empty, which action should the operating system take first?
š Explanation: The correct choice is to perform page replacement because it frees just one frame, allowing the faulting process to continue while keeping other processes running. Terminating the process would waste work and hurt user experience. Swapping out a whole process frees many frames but reduces multiprogramming and can be excessive for a single fault. Expanding the I/O buffer pool does not address the lack of free frames for paging. Page replacement solves the immediate shortage with minimal disruption, matching typical OS behavior.
Q2. What is the main purpose of a pageāreplacement algorithm in a demandāpaging system?
š Explanation: The algorithmās goal is to select a frame that can be reclaimed so the newly needed page can be loaded, which directly supports demand paging. Increasing physical memory is a hardware concern, not an algorithmic task. Compressing disk data is unrelated to paging decisions. CPU scheduling is handled by a separate scheduler. Therefore, freeing a frame for the new page correctly captures the purpose of pageāreplacement policies.
Q3. In a multitasking environment where many processes generate frequent page faults, which replacement strategy typically reduces thrashing the most?
š Explanation: LeastāRecentlyāUsed approximates the optimal algorithm by discarding pages that have not been used recently, which tends to keep frequently accessed pages in memory and thus cuts down on repeated faults (thrashing). FIFO may keep heavily used pages out for too long, leading to more faults. The true optimal algorithm cannot be implemented because it requires future knowledge. Random replacement ignores usage patterns, making thrashing more likely. Hence LRU is the practical choice for minimizing thrashing.
Q4. Which term describes the condition where the operating system spends most of its time swapping pages rather than executing user processes?
š Explanation: Thrashing is the specific condition in which excessive paging activity dominates CPU time, leaving little work for user programs. Paging itself is the general mechanism of moving pages between memory and disk, not the pathological overload. Fragmentation refers to wasted space, and segmentation is a different memoryāmanagement scheme. Therefore, thrashing precisely captures the situation of heavy swapping activity.
Q5. Why does demand paging improve overall system utilization compared to allocating all memory up front?
š Explanation: Demand paging lets a process start execution with only the pages it immediately needs, freeing the rest of memory for other processes. This sharing raises overall utilization because unused pages do not occupy valuable frames. The presence of a pageāreplacement algorithm is still necessary when frames fill up, so option A is incorrect. While demand paging can reduce the total physical memory needed (option C), the primary benefit is concurrent use of memory across processes. It does not prevent page faults; rather, it tolerates them when they occur.
Q6. A machine has 100 physical frames, 30 of which are permanently assigned to I/O buffers. A new process needs 5 additional pages, but all remaining frames are occupied. Which step should the OS perform to satisfy the request?
š Explanation: Applying a pageāreplacement algorithm frees exactly one frame, enabling the new pages to be loaded without disrupting other processes more than necessary. Giving more frames to I/O buffers would reduce the pool available for user pages, worsening the shortage. Swapping out an entire process is drastic and would lower system throughput. Terminating the process discards work and is undesirable when a less invasive solution exists. Hence page replacement is the appropriate response.
Q7. If a system keeps a fixed 15āÆ% of memory for I/O buffers and suddenly experiences heavy I/O activity, which policy would most effectively prevent excessive swapping of user processes?
š Explanation: Dynamically expanding the I/O buffer pool gives the I/O subsystem the extra frames it needs during peak activity, keeping user pages from being evicted unnecessarily. Keeping the allocation fixed (option B) would force the OS to reclaim user frames, increasing swapping. Prefetching (option C) helps CPUābound workloads but does not address I/Oādriven memory pressure. A workingāset model (option D) manages perāprocess locality but does not directly allocate more frames to I/O, so it cannot mitigate the immediate shortage caused by heavy I/O.
Q8. Swapping an entire process out of memory versus replacing a single page each have tradeāoffs. Which statement best captures the advantage of page replacement over full process swapping?
š Explanation: Page replacement removes only the selected page, preserving the remainder of the process in memory so it can keep running, which maintains higher overall throughput. Although page replacement still incurs disk I/O, it typically involves a smaller amount of data than swapping an entire process, saving both I/O bandwidth and CPU time. Option A is inaccurate because the overhead depends on the number of pages swapped. Option C reverses the realityāswapping a process involves moving many pages, while page replacement moves just one. Option D is false; page replacement relies on the pageāfault handler to manage faults.
Q9. Consider a FIFO pageāreplacement algorithm with three frames. The reference string is . Which page will be evicted when the reference to page 5 occurs for the first time?
š Explanation: With FIFO, the oldest loaded page is removed first. After processing the first six references (1,2,3,4,1,2), the three frames contain pages 4,1,2 in the order they entered: 4 (oldest), then 1, then 2. When pageāÆ5 is requested, the algorithm discards the oldest frame, which holds pageāÆ4? Wait: actually after reference 4, frames are [1,2,3] replaced by 4 evicting 1 (FIFO). Then references 1 and 2 bring pages back, evicting 2 and 3 respectively. By the time 5 is needed, the oldest resident is pageāÆ3, so pageāÆ3 is evicted. Thus the correct answer is pageāÆ3.
Q10. How does allocating too large a portion of memory to I/O buffers increase the likelihood of page faults for user processes?
š Explanation: When a disproportionate share of physical memory is dedicated to I/O buffers, fewer frames remain for user pages. As user processes request new pages, the scarcity of free frames forces the OS to replace pages that may still be needed, raising the fault rate. The allocation does not disable demand paging (option B) nor directly affect CPU speed (option C). While fragmentation can be an issue, the primary mechanism here is simple reduction of available frames, making option A the correct explanation.
Q11. An 8āframe system currently uses 2 frames for I/O buffers, leaving 6 frames for processes. ProcessāÆA occupies pages {1,2,3} and ProcessāÆB occupies pages {4,5,6}. A new page 7 is requested and no free frame exists. Which replacement choice is most likely to maintain the highest overall throughput?
š Explanation: Replacing a page from ProcessāÆA is preferable because it frees a single frame while keeping both processes alive, preserving concurrency and throughput. Removing an I/O buffer frame (option A) would impair I/O performance, potentially throttling the entire system. Swapping out all of ProcessāÆB (option D) would drastically reduce multiprogramming and increase latency. Replacing a page from ProcessāÆB (option C) is equally valid but may be less optimal if ProcessāÆBās pages are more recently used; choosing the lessārecently used page from ProcessāÆA typically yields better overall performance.