The OS solves most space-management problems one of two ways. Chop things into variable-sized pieces β segmentation β and the space itself fragments. Or chop into fixed-sized pieces: in virtual memory this is paging paging Virtualizing memory by chopping the address space into fixed-sized pages mapped to physical page frames β no external fragmentation by design, and sparse address spaces come cheap (Atlas, early 1960s). defined in ch. 18 β open in glossary , an idea going back to the Atlas (early 1960s). The address space becomes fixed-size pages page A fixed-sized unit of a virtual address space; every page can live in any free physical frame, which is what makes free-space management trivial under paging. defined in ch. 18 β open in glossary ; physical memory becomes an array of page frames page frame A fixed-sized slot of physical memory holding exactly one virtual page; physical memory is viewed as an array of them. defined in ch. 18 β open in glossary , each holding exactly one page.
The Crux: How To Virtualize Memory With Pages
How can we virtualize memory with pages, so as to avoid the problems of segmentation? What are the basic techniques? How do we make those techniques work well, with minimal space and time overheads?18.1 A Simple Example And Overview
A tiny example: a 64-byte address space of four 16-byte pages, placed into a 128-byte physical memory of eight frames. Follow the colors β each page landed wherever a free frame happened to be:
click a region marked β for details
click a region marked β for details
Two advantages are already visible. Flexibility: the system supports the address-space abstraction regardless of how the process uses it β no assumptions about which way the heap and stack grow. Simplicity: placing the address space took no fit policy, no splitting, no coalescing β the OS keeps a free list of pages and grabbed the first four (frames 3, 7, 5, 2). Any free frame serves any page; last chapterβs whole struggle dissolves.
To record where each virtual page landed, the OS keeps a per-process structure β the page table page table The per-process structure storing each virtual page's translation (VPN β PFN) plus per-page bits; one of the most important data structures in a modern OS β and, done naively, both too big and too slow. defined in ch. 18 β open in glossary β storing one translation per virtual page: (VP 0 β PF 3), (VP 1 β PF 7), (VP 2 β PF 5), (VP 3 β PF 2). Per-process matters: another process has its own table, its virtual pages mapping to different frames (sharing aside).
Translation. Suppose the process executes movl 21, %eax. Split
the virtual address into two components: the virtual page number
(VPN) β the top bits, enough to name every page (4 pages β 2 bits) β
and the offset within the page (16 bytes β 4 bits). Then replace
the VPN with the frame number the page table reports, and keep the
offset exactly as-is β it names a byte within the page, and the page
moved as one intact unit:
virtual address
21
010101
MMU (paging)
1. VPN = top 2 bits = 1 Β· offset = 5
2. PT[1] β PFN 7 β
3. PA = 7Γ16 + 5 = 117
physical address
117
| VPN | β PFN | valid |
|---|---|---|
| 0 | 3 | 1 |
| 1 β | 7 | 1 |
| 2 | 5 | 1 |
| 3 | 2 | 1 |
The bookβs example: 21 = 010101 β VPN 01 (page 1), offset 0101 (byte 5); PT[1] says PFN 7 (binary 111); result 1110101 = 117. Then: 0 β 48 (page 0 starts at frame 3), 50 β 34 (page 3 lives DOWN at frame 2 β scatter in action), 63 β 47 (the very last byte). The VPN chunkβs color matches the page colors in the maps above.
Virtual 21 = binary 010101: VPN 01 β page 1; page table says
PFN 7 (111); physical address 1110101 = 117 β byte 5 of frame
7, precisely where Figure 18.2 drew page 1βs bytes.
With the overview in hand, the beguiling questions queue up: where are these page tables stored? What exactly is in them, and how big do they get? Doesnβt this make everything slow? Read on.
Check yourself
1.Segmentation chopped the address space into variable-sized pieces. What does paging's fixed-size choice buy, immediately?
2.Translate virtual address 21 in the 64-byte address space (16-byte pages; page table: 0β3, 1β7, 2β5, 3β2).
3.Why does the offset pass through translation completely unchanged?
4.Process A and process B both use virtual page 0. How do they not collide?
5.The 64-byte space needs a 2-bit VPN and 4-bit offset. A 32-bit address space with 4KB pages needsβ¦