When the CPU issues an address whose PTE valid bit is 0, the MMU traps. The OS service routine performs these steps:
Trap to the OS. Hardware saves the faulting instruction's state (PC, registers) so it can be restarted later.
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.
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.
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).
Disk I/O completes → interrupt. OS updates: the page table now maps the virtual page to the new frame, and sets ==valid bit = 1==.
Restart the instruction that caused the fault. Now the access succeeds and execution resumes as if nothing happened.
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. ⇒ (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!
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=(1−p)m+ps. Yahan m normal RAM access time (nanoseconds), p fault hone ki probability, aur s fault service time (disk ki wajah se milliseconds). Kyunki s bahut hi bada hai m ke comparison me, thoda sa bhi p 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.