šŸŽ“ BookMCQ
← Back to 9. Virtual Memory

šŸ“ 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.
2
Easy
4
Medium
5
Hard

šŸ“ 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?

A.Terminate the process immediately
B.Swap out an entire process to free all its frames
C.Perform page replacement to free a single frame āœ…
D.Increase the size of the I/O buffer pool
šŸ’” Difficulty: easy | āœ… Correct: C

šŸ“– 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?

A.To increase the size of physical memory
B.To free a memory frame for a newly requested page āœ…
C.To compress data stored on disk
D.To schedule CPU time among processes
šŸ’” Difficulty: easy | āœ… Correct: B

šŸ“– 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?

A.First‑In‑First‑Out (FIFO)
B.Optimal (theoretical) algorithm
C.Least‑Recently‑Used (LRU) āœ…
D.Random replacement
šŸ’” Difficulty: medium | āœ… Correct: C

šŸ“– 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?

A.Paging
B.Fragmentation
C.Thrashing āœ…
D.Segmentation
šŸ’” Difficulty: medium | āœ… Correct: C

šŸ“– 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?

A.It eliminates the need for a page‑replacement algorithm āœ…
B.It allows processes to run even if some of their pages are not currently in memory
C.It reduces the amount of physical memory required by the system
D.It guarantees that no page faults will ever occur
šŸ’” Difficulty: medium | āœ… Correct: A

šŸ“– 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?

A.Allocate more frames to the I/O buffers
B.Swap out an entire process to free its frames
C.Terminate the requesting process
D.Apply a page‑replacement algorithm to evict a single page āœ…
šŸ’” Difficulty: medium | āœ… Correct: D

šŸ“– 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?

A.Dynamically increase the memory portion reserved for I/O buffers
B.Maintain the fixed 15 % allocation regardless of load āœ…
C.Prefetch pages for all active processes
D.Adopt a working‑set model for each process
šŸ’” Difficulty: hard | āœ… Correct: B

šŸ“– 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?

A.Page replacement always uses less CPU time than swapping a process āœ…
B.Page replacement can free memory while allowing the rest of the process to continue executing
C.Swapping a process requires no disk I/O, while page replacement does
D.Page replacement eliminates the need for a page‑fault handler
šŸ’” Difficulty: hard | āœ… Correct: A

šŸ“– 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 1,2,3,4,1,2,5,1,2,3,4,51,2,3,4,1,2,5,1,2,3,4,5. Which page will be evicted when the reference to page 5 occurs for the first time?

A.Page 1
B.Page 2
C.Page 3 āœ…
D.Page 4
šŸ’” Difficulty: hard | āœ… Correct: C

šŸ“– 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?

A.It reduces the number of frames available for user pages, causing more competition for memory
B.It causes the operating system to disable demand paging āœ…
C.It forces the CPU to execute I/O operations more slowly, leading to more faults
D.It creates fragmentation that prevents pages from being loaded
šŸ’” Difficulty: hard | āœ… Correct: B

šŸ“– 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?

A.Replace a frame used by the I/O buffers āœ…
B.Replace a page belonging to Process A
C.Replace a page belonging to Process B
D.Swap out all pages of Process B
šŸ’” Difficulty: hard | āœ… Correct: A

šŸ“– 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.

šŸ”— Related Topics (MCQs)