Virtual Memory & Demand Paging

page-fault service

Imagine a librarian's routine when you ask for a book that is not on the open shelves. She checks the catalogue to confirm the book is real and find where it is stored. She walks to the closed stacks (or requests it from off-site). She clears a spot on the return cart if it is full, perhaps re-shelving a book nobody has touched in a while. She fetches your book, records that it is now on the shelf, and finally tells you to go on reading from where you left off. Page-fault service is the operating system performing this exact sequence to satisfy a missing page.

Concretely, when a page fault traps to the OS, it carries out a fixed sequence of steps. First, it checks an internal table to decide whether the reference is legal at all; if the address is outside the process's space, it terminates the process. If the reference is legal but the page is simply not resident, it finds a free frame from the free-frame list. If no frame is free, it must select a victim page to evict, and if that victim has been modified, write it back to the backing store first (page-out). Next it schedules a disk read to bring the wanted page from the backing store into the chosen frame; while the disk works, the CPU can run other processes. When the read completes, the OS updates the process's page table to mark the page valid and point at the new frame, restores the process to the ready queue, and arranges for the faulting instruction to be restarted so the access can now succeed.

Why it matters: this sequence is the whole machinery of demand paging, and almost all of its cost is the disk read in the middle. The CPU work — trapping, checking tables, updating entries — is fast; the disk access is glacial by comparison. That is why the cost of servicing a fault is measured in milliseconds while ordinary memory access is measured in tens of nanoseconds. The honest implication: every avoidable fault is enormously expensive, which is why the whole design leans so hard on locality (so faults are rare) and on smart page replacement (so the victim chosen is one unlikely to be needed soon).

Steps for one fault: (1) trap to OS; (2) check the reference is legal; (3) find a free frame, or evict a victim (writing it back first if it is dirty); (4) issue a disk read into that frame; (5) on completion, set the page-table entry to valid; (6) restart the faulting instruction.

The disk read in the middle dominates the entire service time.

A fault can cost two disk transfers, not one: if the chosen victim frame holds a modified (dirty) page, the OS must write that page out before it can read the wanted page in. Choosing clean victims when possible halves this cost — one reason the dirty bit exists.

Also called
page-fault handler頁錯誤服務常式缺頁處理