Sub topic 8
Virtual Memory and Demand Paging
This chapter assumes you know how paging translates an address: a virtual address splits into a page number and an offset, and the page table maps the page number to a frame number. You should also be comfortable with powers of two and with the cost gap between RAM and disk. Here you will see how the operating system uses that machinery to run programs that are not fully in memory.
Why Virtual Memory Exists
Plain paging gives each process its own address space, but it still requires every page of a running process to sit in a frame. That has three costs:
- A program larger than physical memory cannot run.
- Much of a program's code is rarely used, such as error handlers and unused features. Loading it wastes frames.
- Fewer processes fit in memory at once, so the CPU is idle more often.
Virtual memory removes the requirement. A process sees a large, uniform address space, while the OS keeps only the pages currently needed in RAM. The rest stay on disk in a backing store, usually called swap space for modified data and the executable file for unmodified code.
This works because of locality of reference. Over a short window, a program touches a small set of pages, and it touches pages near recently used ones. If locality did not hold, virtual memory would be unusable. Chapter 14 returns to this when it covers thrashing.
Demand Paging
Demand paging is the most common way to implement virtual memory. The OS loads a page into a frame only when the process first touches it, so loading is lazy.
In pure demand paging, a process starts with no pages in memory. The first instruction fetch faults, then the first stack access faults, and so on, until the working pages are resident. Real systems often load a few pages up front, which is called prepaging, but the GATE model is the pure version.
Demand paging needs hardware support:
- A page table with a valid-invalid bit in each entry.
- Secondary storage that holds the pages not in memory.
- The ability to restart an instruction after a fault.
The Page Table Entry
Each page table entry holds more than a frame number.
| Field | Meaning |
|---|---|
| Frame number | Where the page sits in RAM. It is meaningful only if the valid bit is set. |
| Valid bit | 1 means the page is in memory. 0 means it is not in memory, or it is not part of the process's address space. |
| Dirty (modify) bit | Set by hardware on a write. A clean page can be dropped without writing it to disk. |
| Reference bit | Set by hardware on any access. Used by replacement approximations. |
| Protection bits | Read, write and execute permissions. |
The valid bit alone cannot tell the OS why it is 0. The OS keeps extra information per process, such as the list of regions the process owns. It uses that information to decide between two cases:
- The address is outside the process's legal regions. This is a protection error, and the OS terminates the process (a segmentation fault).
- The address is legal but the page is not resident. This is a genuine page fault, and the OS brings the page in.
Handling a Page Fault
A page fault is a trap raised by the MMU when translation hits an invalid entry. The steps are shown below.
While the disk read runs, the OS usually switches the CPU to another process. When the disk finishes, it raises an interrupt, and the faulting process becomes ready again. The "choose a victim" step is the subject of page replacement. The cost of the whole sequence is the subject of the next chapter.
Restarting the Instruction
After the fault is served, the instruction runs again from the beginning. This is easy for a simple load or store. It is harder when one instruction touches several memory locations, for example a block move whose source and destination ranges overlap. If a fault occurs halfway, part of the destination has already been overwritten.
There are two standard fixes:
- The hardware first touches both ends of the source and destination ranges, so any fault happens before data changes.
- The hardware saves the old values of the modified locations in temporary registers and restores them if a fault occurs.
A Worked Example
Take a 32-bit virtual address, 4 KB pages, 1 GB of physical memory and 4-byte page table entries.
- The offset needs 12 bits, so the page number needs 32 − 12 = 20 bits. The process has 2^20 pages, which is 4 GB of virtual address space.
- Physical memory has 2^30 / 2^12 = 2^18 frames. A frame number needs 18 bits, so a 4-byte entry has 14 bits left for the valid, dirty, reference and protection bits.
- The page table holds 2^20 entries of 4 bytes each, so it takes 4 MB per process.
A process can use a 4 GB address space on a machine with 1 GB of RAM, because only some of its pages need frames at any moment. The page table size depends on the virtual address space, not on the physical memory. That is the reason chapter 6 introduces multilevel tables.
Simulating Lazy Loading
The program below models pure demand paging when memory is large enough that nothing is ever evicted. It counts one fault for each distinct page touched, which is the minimum number of faults any run can have.
def demand_faults(addresses, page_size=4096):
resident = set() # pages whose valid bit would be 1
faults = 0
for va in addresses:
page = va // page_size
if page not in resident:
faults += 1 # first touch: trap, load, set valid bit
resident.add(page)
return faults
addrs = [0x0000, 0x0FFF, 0x1000, 0x1004, 0x0010, 0x5000, 0x1FFF]
print(demand_faults(addrs)) # 3 (pages 0, 1 and 5)Seven references cause only three faults. The addresses 0x0000, 0x0FFF and 0x0010 share page 0, and 0x1000, 0x1004 and 0x1FFF share page 1. This is locality at work. With a limited number of frames, the count can only go up, and the choice of replacement algorithm decides by how much.
Benefits and Costs
The benefits are:
- A program can be larger than physical memory.
- Programs start faster, because only the first pages are loaded.
- More processes can be resident, which raises CPU utilization.
- Unused code and data never cost a frame or a disk read.
The costs are:
- A fault costs milliseconds, against nanoseconds for a normal memory access.
- The OS needs a policy for choosing victims and for splitting frames among processes.
- If too many processes compete for too few frames, the system spends its time paging and does little useful work.
Common Errors
- Treating a page fault as an error. A fault on a legal address is a normal event. Only a reference to an illegal address terminates the process.
- Confusing the valid bit with the protection bits. The valid bit says whether the page is present and legal. Protection bits say which kinds of access are allowed to a present page. A write to a read-only page raises a protection fault even though the valid bit is 1.
- Sizing the page table from physical memory. The number of entries is the number of virtual pages. Physical memory only decides how many bits the frame number needs.
- Writing every victim back to disk. Only dirty pages must be written. A clean page can be discarded, and its frame reused at once.
- Assuming virtual memory makes memory free. Virtual memory hides the limit but does not remove it. When the combined working sets exceed RAM, performance collapses.
- Forgetting that the instruction restarts. After the OS serves the fault, the same instruction runs again, and the second attempt must succeed.
Exercises
Exercise
A system has 40-bit virtual addresses, 16 KB pages, 4 GB of physical memory and 8-byte page table entries. How many bits does the page number need, how many bits does the frame number need, and how large is the single-level page table in MB?
Show answer
16 KB is 2^14, so the offset is 14 bits and the page number is 40 − 14 = 26 bits. Physical memory is 2^32 bytes, so there are 2^32 / 2^14 = 2^18 frames and the frame number needs 18 bits. The table has 2^26 entries of 8 bytes, which is 2^29 bytes = 512 MB. The size is set by the virtual address space, so a table this large is impractical and motivates multilevel page tables.
Exercise
A process in a pure demand-paging system issues the page references 3, 1, 3, 2, 1, 4, 2, 3, 5, 1. It has 6 frames available. How many page faults occur, and why is no replacement needed?
Show answer
The distinct pages are 1, 2, 3, 4 and 5, which is 5 pages. Each page faults once on its first reference and stays resident, so there are 5 faults. Because 6 frames are enough to hold all 5 distinct pages, no page is ever evicted and no replacement algorithm is needed.
Exercise
A page-table entry has valid bit 0. Name the two possible causes, and say how the OS tells them apart and what it does in each case.
Show answer
Either the page is legal but not in memory, or the address is not part of the process's address space. The OS consults its own per-process records of legal regions. For a legal page, it finds a frame (evicting a victim if needed), reads the page from disk, sets the frame number and valid bit, and restarts the instruction. For an illegal address, it terminates the process.