Demand paging and page faults
Demand paging and page faults
Recorded by Dr. Krithika L.B. Slot m4-demand-paging
Running programs that are only partly in memory: demand paging, the six steps of handling a page fault, effective access time, and copy-on-write.
By the end of this session you should be able to:
Programs rarely need all their code at once: error-handling routines, rarely used features and over-sized arrays sit unused. If a program could run with only part of it in memory:
The separation of the user's logical memory from physical memory, allowing an extremely large virtual address space to be provided on a smaller physical memory. Only the parts in use need to be in RAM; the rest stays on disk.
Load a page only when it is needed (on demand). A lazy swapper — called a pager — never brings a page into memory unless it is referenced. The page table's valid–invalid bit now means: valid = page is legal and in memory; invalid = page is either not legal or currently on disk.
Accessing a page marked invalid traps to the OS — a page fault.
The process executes load M; the MMU finds page of M marked invalid.
Hardware traps to the OS (an exception, Session 1.4). The OS checks an internal table: invalid reference → terminate; valid but not in memory → page it in.
Find a free frame (if none, run page replacement — Session 4.6).
Schedule a disk read of the page into the frame. The process waits (state waiting); the CPU runs someone else.
When the read completes (I/O interrupt), update the page table: frame number and valid.
Restart the instruction that faulted. It now finds the page in memory as if it had always been there.
Pure demand paging starts a process with no pages in memory — even the first instruction faults. Thanks to locality of reference (programs use a small set of pages at a time), the fault rate quickly drops.
Let p be the page-fault rate (0 ≤ p ≤ 1):
\[ \text{EAT} = (1-p) \times \text{memory access} + p \times \text{page-fault service time} \]
Memory access = 200 ns, page-fault service time = 8 ms = 8 000 000 ns.
EAT = (1 − p) × 200 + p × 8 000 000 = 200 + 7 999 800 p ns.
If 1 access in 1000 faults (p = 0.001): EAT ≈ 200 + 7 999.8 ≈ 8.2 µs — a slowdown by a factor of 40!
To keep degradation below 10% (EAT < 220 ns): 7 999 800 p < 20 → p < 0.0000025 — fewer than 1 fault in 400 000 accesses. Page-fault rate must be kept extremely low.
Recall that fork() copies the parent's address space (Session 2.2) — wasteful if the child immediately calls
exec(). With copy-on-write (COW), parent and child initially share all pages, marked read-only. Only
when either process writes to a shared page is a private copy of that page made. Unmodified pages are never copied.
Child's page table points to the parent's frames; all marked copy-on-write.
The child writes to page C → protection fault → kernel copies C into a new frame for the child.
Demand paging and page faults
Recorded by Dr. Krithika L.B. Slot m4-demand-paging
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 00.9999 × 100 + 0.0001 × 10 000 000 ≈ 99.99 + 1000 ≈ 1.1 µs.
Starting a process with no pages in memory and bringing in every page only when first referenced.
Locality of reference: a process uses a small set of pages for a while, so faults are rare after the start.
Only pages that the parent or child actually writes to.
Step 3 said “find a free frame”. When memory is full, the OS must evict a page — and choosing badly can bring the system to a crawl. Next: page replacement and thrashing.
Continue to 4.6 — Page replacement, thrashing and the working set →