Sub topic 8
Virtual Memory and Demand Paging
Prerequisites and the Working Model
This chapter assumes that you understand binary and hexadecimal numbers, process basics, main memory, disk storage, and short C or Python programs. You should also know that a process uses addresses while the operating system and memory hardware store data in physical memory.
Virtual memory is a memory-management technique that gives a process a large, private address space even when physical memory is smaller. The process generates a virtual address. The memory-management unit (MMU) translates it into a physical address.
A process is divided into fixed-size pages. Physical memory is divided into same-size frames. A page table records which frame contains each virtual page.
Demand paging adds an important rule: load a page into a frame only when the process actually references it. Pages that have not yet been used can remain on secondary storage, such as an SSD.
What Happens on a Page Reference
Each page-table entry usually includes a frame number and a present bit. The present bit says whether the page currently resides in physical memory.
- If the present bit is set, translation continues normally.
- If the present bit is clear, the CPU raises a page fault.
- If the address is outside the process's legal range, the operating system reports an invalid access instead of loading a page.
A page fault is a trap into the operating system. It does not necessarily mean that the program is wrong. A valid page can simply be absent from physical memory.
Page-Fault Handling
The operating system handles a valid page fault in several stages:
- Save the process state and identify the faulting virtual address.
- Check the page table and process permissions.
- Locate a free frame. If none exists, choose a resident page to remove.
- If the removed page was modified, write it back to disk.
- Read the required page from disk into the selected frame.
- Update the page-table entry and its present bit.
- Restart the instruction that caused the fault.
The instruction is restarted because the memory access did not complete. The operating system must preserve the instruction's intended effect, so it normally does not execute the instruction halfway and then continue.
Locality and the Cost of Demand Paging
Programs commonly show locality, meaning that references tend to cluster:
- Temporal locality: a recently used instruction or data item is likely to be used again.
- Spatial locality: addresses near a recently used address are likely to be used soon.
Locality allows a process to run with only part of its address space in memory. The operating system can keep frequently used pages resident and fetch other pages when needed. However, if page faults occur constantly, execution can spend more time waiting for storage than running instructions. The later topics on page replacement and thrashing analyze this problem in detail.
Exercise
Exercise
A process references a legal page whose present bit is clear. What are the two main actions the operating system must perform before retrying the instruction?
Show answer
It must bring the page from secondary storage into a physical frame and update the page table, including setting the page's present bit.