What is a page fault?

Why Interviewers Ask This

This is a classic screening question for Operating Systems roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

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.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Operating Systems codebase.