RTUComputer ScienceYr 2023 · Sem 52023

Q4Operating System

Question

10 marks

Explain the concept of Virtual Memory in detail. Discuss the Page Replacement Algorithms with examples.

Answer

An exhaustive architectural analysis of Virtual Memory and Demand Paging, explicitly detailing the catastrophic hardware Page Fault mechanism and comparing the FIFO, LRU, and Optimal Page Replacement algorithms.

In the primitive era of computing, the absolute physical size of a program (e.g., a 10MB game) could never exceed the absolute physical size of the motherboard's RAM (e.g., 4MB). Virtual Memory is a massively complex mathematical illusion engineered by the OS Kernel and the CPU's Memory Management Unit (MMU) to completely shatter this limitation. Virtual Memory allows the execution of processes that are astronomically larger than physical RAM by mathematically utilizing the hard drive as an extension of memory.

The Mechanics of Demand Paging

Virtual Memory relies on Demand Paging. The 10MB program is mathematically sliced into 4KB blocks called "Pages". When the user double-clicks the game, the OS does NOT load the 10MB into RAM. It lazily loads ONLY the very first page containing the main() function into a RAM "Frame".

As the CPU executes, it will inevitably attempt to jump to a line of code located on Page 5. The MMU checks the Page Table and realizes Page 5 is NOT in RAM; it is still on the hard drive. This triggers a catastrophic hardware exception known as a Page Fault. The OS violently halts the program, commands the hard drive to read Page 5, loads it into a free RAM Frame, updates the Page Table, and restarts the CPU instruction flawlessly.

The crisis occurs when the physical RAM is 100% full, and a Page Fault triggers. The OS must violently evict an existing page from RAM to make room. The algorithm chosen drastically impacts system speed.

1. FIFO (First-In, First-Out)

The OS tracks the exact time each page entered RAM. It ruthlessly evicts the absolute oldest page. - Demerit: It is mathematically blind. It might evict the core OS kernel loop just because it was loaded early. It also suffers from Belady's Anomaly, where physically adding more RAM to the motherboard can bizarrely cause more page faults.

2. LRU (Least Recently Used)

This is the industry standard. It relies on the mathematical principle of Temporal Locality (if you used a variable 1 millisecond ago, you will probably use it again soon). - Mechanism: The OS utilizes complex hardware counters to track the exact microsecond every single page was last accessed by the CPU. When eviction is necessary, it violently hunts down the page with the absolute oldest access timestamp (the one completely ignored by the program) and evicts it. It provides near-perfect performance.

3. Optimal Page Replacement (OPT)

This is a theoretical, mathematically perfect algorithm. - Mechanism: It evicts the page that will not be used for the longest period of time in the absolute FUTURE. - Reality: It is physically impossible to implement because the OS cannot mathematically predict the future execution path of the code. It is used strictly as a theoretical benchmark to measure how good LRU is.

Back to Paper