4.2.28Operating Systems

Demand paging — page fault handling steps

2,150 words10 min readdifficulty · medium5 backlinks

WHY does demand paging exist?


WHAT is a page fault?


HOW: The page-fault handling steps (the heart of this note)

When the CPU issues an address whose PTE valid bit is 0, the MMU traps. The OS service routine performs these steps:

  1. Trap to the OS. Hardware saves the faulting instruction's state (PC, registers) so it can be restarted later.
  2. Check legality. OS consults an internal table (often the VMA/segment list): is this reference to a valid part of the address space, or a genuinely illegal address?
    • Illegal ⇒ terminate the process (segmentation fault).
    • Valid but not in memory ⇒ continue.
  3. Find a free frame from the free-frame list.
    • If no free frame ⇒ run page replacement (e.g. LRU/Clock) to evict a victim. If the victim is dirty, write it back to disk first.
  4. Schedule disk read. Issue I/O to read the needed page from the backing store (disk/swap) into the chosen frame. The process blocks (CPU is given to another process meanwhile).
  5. Disk I/O completes → interrupt. OS updates: the page table now maps the virtual page to the new frame, and sets ==valid bit = 1==.
  6. Restart the instruction that caused the fault. Now the access succeeds and execution resumes as if nothing happened.
Figure — Demand paging — page fault handling steps

Worked examples


Common mistakes (Steel-man + fix)


Active recall

Recall Quick self-test (hide answers, predict first — Forecast-then-Verify)
  • What bit does the MMU check on every access? ⇒ the valid/present bit.
  • Two outcomes of the legality check? ⇒ terminate (illegal) / fetch page (valid-absent).
  • Why restart the instruction instead of advancing? ⇒ it never completed.
  • When can we skip writing the victim to disk? ⇒ when its dirty bit = 0 (clean).
  • Write EAT. ⇒ (1p)m+ps(1-p)m + ps.
What is demand paging?
A lazy-loading scheme where a page is brought into RAM only when it is first referenced, not at load time.
What triggers a page fault?
A memory access to a page whose PTE valid/present bit = 0 (page not in a physical frame).
Is a page fault always an error?
No — only if the address is illegal. A valid-but-absent page is the normal trigger for demand paging.
List the page-fault handling steps.
1) Trap to OS, save state. 2) Check legality. 3) Find a free frame (replace if none). 4) Schedule disk read; process blocks. 5) On I/O completion update PTE & set valid bit. 6) Restart the faulting instruction.
What happens if there is no free frame?
Run page replacement to evict a victim; if victim is dirty, write it back to disk first.
Why must the OS restart (not advance past) the faulting instruction?
The instruction never completed; hardware saved its state so it is re-executed and now succeeds.
Give the Effective Access Time formula.
EAT = (1-p)·m + p·s, where m = memory access time, p = page-fault rate, s = fault service time.
Why does a tiny page-fault rate cripple performance?
Because service time s (≈ms, disk) is millions of times larger than m (≈ns), so the term p·s dominates EAT.
What does the dirty bit let the OS avoid?
Skipping the write-back to disk when a clean victim page is evicted.
What is the backing store in demand paging?
The disk/swap area holding pages that are not currently in physical memory.
Recall Feynman: explain to a 12-year-old

Imagine your backpack (RAM) is small but your locker (disk) is huge. You don't carry every book all day — you only grab a book from the locker the moment you need it. When you reach for a book that's not in your bag, you "pause," walk to the locker, swap a book you don't need for the one you do, and then redo the page you were trying to read. That pause-and-fetch is a page fault, and the whole grab-only-when-needed habit is demand paging. Walking to the locker takes way longer than reading from your bag, so you really don't want to forget books too often!


Connections

  • Virtual Memory — demand paging is its core mechanism.
  • Page Replacement Algorithms — invoked at step 3 when no free frame (LRU, Clock, FIFO).
  • Page Table / TLB — where the valid & dirty bits live; TLB miss vs page fault.
  • Thrashing — what happens when pp stays too high.
  • Belady's Anomaly — a quirk of some replacement policies.
  • Context Switching — the process blocks during step 4's disk I/O.

Concept Map

marks pages absent

valid bit 0 triggers

traps to OS

illegal address

valid not in memory

no free frame

dirty victim written back

frame ready

I/O completes

resume execution

page-fault rate p affects

Demand paging: lazy loading

Page-table entry with valid bit

Page fault trap

Check legality

Terminate process

Find free frame

Page replacement evicts victim

Schedule disk read

Update PTE, valid bit = 1

Restart instruction

Effective Access Time

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Demand paging ka simple funda yeh hai: program ki saari pages ko shuru me hi RAM me load mat karo. Sirf wahi page lao jab program usko actually touch kare. Page table me har page ke saath ek valid bit hota hai — agar 1 hai to page RAM me hai, agar 0 hai to page abhi disk pe hai. Jab CPU aisi page maange jiska valid bit 0 ho, to hardware ek trap maarta hai jisko hum page fault kehte hain. Yaad rakho — page fault koi error nahi hai, yeh to normal trigger hai.

Page fault hone par OS ye steps follow karta hai: (1) Trap aata hai, instruction ka state save hota hai. (2) Check karo address legal hai ya nahi — illegal hua to process terminate (segfault), warna aage badho. (3) Ek free frame dhundo; agar koi free na ho to page replacement chalao aur victim ko evict karo (agar victim dirty hai to pehle disk pe write back karo). (4) Disk se zaroori page read karo — is dauran process block ho jaata hai aur CPU kisi aur ko mil jaata hai. (5) Read complete hone par page table update karo aur valid bit 1 set karo. (6) Jo instruction fault kara thi usko restart karo — ab access successful ho jaata hai.

Performance ka maths bhi samajh lo: EAT=(1p)m+ps\text{EAT} = (1-p)m + ps. Yahan mm normal RAM access time (nanoseconds), pp fault hone ki probability, aur ss fault service time (disk ki wajah se milliseconds). Kyunki ss bahut hi bada hai mm ke comparison me, thoda sa bhi pp badhe to EAT phat jaata hai. Isiliye OS chahta hai ki page faults bilkul rare hon — warna thrashing ho jaata hai aur system slow pad jaata hai. Bas yaad rakho mnemonic: "Tom Caught Five Red Ugly Rats" = Trap, Check, Find, Read, Update, Restart.

Test yourself — Operating Systems

Connections