DEV Community

Cover image for Why Do Page Faults Happen? The Demand Paging Lifecycle
Binary Dose
Binary Dose

Posted on Originally published at binarydose.in

Why Do Page Faults Happen? The Demand Paging Lifecycle

Why Do Page Faults Happen? The Demand Paging Lifecycle

When an operating system launches a 2GB application, it does not copy 2GB of machine code from your SSD into physical RAM.

Instead, modern kernels practice Demand Paging—pages of memory are loaded lazily, only when the CPU actually attempts to read or execute an address in that page.


⚡ The 6-Step Page Fault Sequence

[ CPU ] ──(Virtual Address)──► [ MMU ]
                                   │ (Valid Bit = 0)
                                   ▼
                            [ TRAP #14 ]
                                   │
                                   ▼
                 [ OS Kernel: Page Fault Handler ]
                                   │
         ┌─────────────────────────┴─────────────────────────┐
         ▼                                                   ▼
[ Free Frame Allocation ]                         [ Disk DMA Read (4KB) ]
         │                                                   │
         └─────────────────────────┬─────────────────────────┘
                                   ▼
                     [ Page Table: Valid = 1 ]
                                   │
                                   ▼
                      [ Restart Instruction ]
Enter fullscreen mode Exit fullscreen mode

1. MMU Translation Failure

When the CPU references an address whose Page Table Entry (PTE) has Valid = 0, the Memory Management Unit (MMU) halts address translation.

2. Hardware Trap #14

The CPU triggers Hardware Exception Trap #14 (Page Fault Exception), elevating privileges from User Mode (Ring 3) to Kernel Mode (Ring 0).

3. Frame Allocation & Disk DMA

The kernel allocates a free physical frame (or evicts one via LRU/Clock algorithms). It then initiates an asynchronous DMA transfer to copy the missing 4KB page from disk into physical RAM.

4. Page Table Update & Restart

The kernel marks Valid = 1, stores the new physical frame address in the PTE, and restarts the faulting instruction. The user program never knows the page was missing!


🔍 Major vs Minor Page Faults

In production systems, not all page faults touch physical disk:

  • Minor Page Fault: The page is already in physical memory (e.g., shared library like libc already mapped by another process, or an unallocated zero-fill page). No disk I/O is required!
  • Major Page Fault: The page must be physically fetched from storage via disk DMA. Stalls the thread for milliseconds (100,000× slower than RAM access).

🚀 Simulate Virtual Memory & Page Faults Interactively

Want to step through the MMU translation process, trigger Trap #14, and inspect page table residency live?

👉 Test the interactive Virtual Memory and Demand Paging simulator on Binary Dose.

Top comments (0)