What is a page fault?
Answer
A page fault occurs when a process tries to access a virtual memory page that is not currently loaded in physical RAM. The CPU generates a page fault exception (trap), which transfers control to the OS's page fault handler. Page fault handling steps: (1) CPU detects invalid bit in page table entry (page not in RAM); (2) CPU generates page fault trap, saves current state; (3) OS fault handler runs; (4) Determine if the access is valid: if address is completely invalid (outside process's virtual space) → segmentation fault, kill process; if valid page but not loaded → proceed; (5) Find a free frame in physical RAM; (6) If no free frame → select a victim frame to evict using a page replacement algorithm; (7) If victim frame is dirty (modified), write to swap space; (8) Load the needed page from disk/swap into the free frame; (9) Update page table entry (set valid bit, set frame number); (10) Restart the instruction that caused the fault. Performance impact: page faults are extremely expensive. Memory access: ~100ns. Page fault with disk I/O: ~10ms = 100,000× slower! Minor vs major page faults: minor fault — page is in memory but page table not updated yet (e.g., shared page, first access to demand-zero page); no I/O needed. Major fault — page must be loaded from disk; I/O required. Thrashing: if a process has too many page faults (insufficient frames), it spends more time doing page I/O than actual computation. Working set model: ensure a process has enough frames for its "working set" (recently used pages) to avoid thrashing.