Every assumption so far has kept address spaces small enough to fit in memory โ all of them, at once. Real systems drop that: they add a level to the memory hierarchy memory hierarchy The layered arrangement of storage โ small fast caches, then RAM, then big slow disks at the bottom; swapping adds that bottom level to the virtual-memory story so address spaces can exceed physical memory. defined in ch. 21 โ open in glossary , a device with more capacity than memory and (necessarily) less speed โ classically a hard disk. (If the big thing were fast, weโd just call it memory.)
The Crux: How To Go Beyond Physical Memory
How can the OS make use of a larger, slower device to transparently provide the illusion of a large virtual address space?Why want this? Ease of use: with a large address space you allocate naturally and never wonder whether memory has room. The contrast is instructive โ older systems used memory overlays, with programmers manually staging code and data into memory before every use. Try imagining it; yuck. And beyond one process, multiprogramming practically demands swapping: early machines could not possibly hold every page of every running program.
21.1 Swap Space
Reserve disk space for moving pages back and forth โ swap space swap space Disk space reserved for pages pushed out of memory, read and written in page-sized units; its size caps how many pages a system can have in use, and the OS tracks each swapped page's disk address (stashed in the PTE's PFN bits). defined in ch. 21 โ open in glossary โ read and written in page-sized units, with the OS remembering each swapped pageโs disk address. A tiny system, mid-juggle:
click a region marked โ for details
click a region marked โ for details
Three processes share four frames, each with the rest of its pages parked on disk; Proc 3 is entirely swapped out (hence: not running). The system is pretending โ convincingly โ that memory is far larger than it is. One footnote: swap isnโt the only disk region in this game. Code pages have a pristine copy inside the program binary on disk, so the OS can discard them from memory freely and re-read them later โ no swap copy needed.
21.2 The Present Bit
The machinery, top down: TLB hit โ as ever, fast. TLB miss โ walk the page table; and now the PTEโs present bit present bit PTE bit saying whether the page is in physical memory or swapped to disk; the hinge on which chapter 21's swapping turns (x86 fuses it with validity into a single P bit). defined in ch. 18 โ open in glossary speaks. Present = 1: the page is in memory, extract the PFN, install, retry. Present = 0: the page is valid but living on disk. Accessing it is a page fault page fault An access to a page that is valid but not present in physical memory (present bit = 0) โ really a page MISS, but the hardware can only raise an exception and let the OS make things better. The OS then fetches the page from swap. defined in ch. 21 โ open in glossary , and the OSโs page-fault handler page-fault handler The OS code that services a page fault: find (or evict for) a free frame, issue the disk read, mark the PTE present with the new PFN, and retry the instruction โ always software, even with hardware-managed TLBs (disk is slow enough that nobody cares, and hardware wants no part of swap I/O). defined in ch. 21 โ open in glossary takes the stage.
Aside: Swapping Terminology
Odd, isnโt it โ a perfectly legal access called a โfaultโ? It should really be a page miss. The name follows the machinery: when hardware meets something it canโt handle, it raises an exception and transfers to the OS โ identical plumbing to a true illegal access. Same trap, same name; different meaning entirely.21.3 The Page Fault
Whatever the TLB style โ hardware-managed or software-managed โ page faults are serviced by the OS, in essentially every system. Step through the whole life of one:
The page is mapped and legal โ but its present bit says 0: the bytes are in swap.
Aside: Why Hardware Doesnโt Handle Page Faults
Hardware designers famously distrust the OS โ yet here they hand it the keys. Two reasons. Performance: the disk is so slow that even an OS executing tons of instructions adds negligible overhead. Simplicity: handling a fault means understanding swap space and issuing disk I/O โ machinery the hardware neither has nor wants. Both arrows point the same way, and even hardware types can be happy.The essential trick sits in the PTE: while a page is absent, the bits that would hold its PFN hold its disk address instead. Fault โ look up the disk address โ read the page into a frame โ flip present to 1, write the real PFN โ retry. And note what the process experiences during the milliseconds of disk work: nothing. Itโs blocked, off the CPU, while other ready processes run โ one more place the OS overlaps I/O with computation to keep the machine busy.
Check yourself
1.Why support address spaces bigger than physical memory at all?
2.Where does the OS keep a swapped-out page's disk location โ and why is swap space not the only disk region involved in paging traffic?
3.The book grumbles that 'page fault' is a misnomer. What actually happened, and why the name anyway?
4.TLB-miss handling split into hardware-managed and software-managed camps โ yet page faults are handled by the OS in essentially ALL systems. Why do hardware designers, who famously distrust the OS, allow it?
5.While the page's disk read is in flight, what happens to the faulting process โ and what does the OS do with the time?