Sub topic 2
Multilevel, Hashed and Inverted Page Tables
This chapter assumes you know binary and hexadecimal numbers, powers of two, and the basic idea of a page table that maps pages to frames.
Multilevel Page Tables
A flat page table for a 32-bit address space with 4 KB pages has 2^20 entries. At 4 bytes each, that is 4 MB per process, and most of it is unused. A two-level table splits the 20-bit page number into a 10-bit outer index and a 10-bit inner index. The outer table has 1024 entries of 4 bytes, so it fits in one 4 KB page. Inner tables exist only for regions the process uses.
Hashed page tables hash the page number to a bucket. Each bucket holds a chain of (page, frame) pairs, and a lookup walks that chain. Inverted page tables keep one entry per physical frame, so their size depends on memory, not on the number of processes. A lookup searches for the (process ID, page) pair, usually with a hash table to speed it up.
def split_two_level(va):
outer = (va >> 22) & 0x3FF # top 10 bits
inner = (va >> 12) & 0x3FF # next 10 bits
offset = va & 0xFFF # low 12 bits
return outer, inner, offset
print(split_two_level(0x12345678)) # (72, 837, 1656)Segmentation and Segmented Paging
A segment is a logical unit such as code, stack, or heap. An address is a pair (segment number, offset). The segment table stores a base and a limit for each segment. If the offset is at or above the limit, the hardware traps. Otherwise the physical address is base + offset.
Suppose segment 2 has base 4000 and limit 600. Address (2, 450) maps to 4450. Address (2, 700) causes a protection trap.
Segmented paging gives each segment its own page table. The segment number selects the table, and the page number and offset then work as in normal paging.
Virtual Memory and Demand Paging
Virtual memory lets a process use more address space than RAM holds. Demand paging loads a page only when the process first touches it. Each page table entry has a valid bit. If the bit is clear, the page is not in memory and the access raises a page fault.
Page Faults and Effective Access Time
Handling a fault means the OS finds a free frame, reads the page from disk, updates the page table, and restarts the instruction. Disk access is slow, so a fault costs far more than a memory access. Effective access time (EAT) averages the two cases:
EAT = (1 - p) × memory access time + p × page fault service time
Here p is the fault rate. With 100 ns memory, an 8 ms fault service time, and p = 0.001, EAT = 0.999 × 100 + 0.001 × 8,000,000 = 8099.9 ns. One fault in a thousand accesses makes memory about 80 times slower.
Exercise
Memory access takes 200 ns, a page fault takes 10 ms, and the fault rate is p = 0.002. What is the effective access time?
Show answer
EAT = 0.998 × 200 + 0.002 × 10,000,000 = 199.6 + 20,000 = 20,199.6 ns, about 20.2 µs.