# Page Fault Handling

> A page fault is a hardware trap raised when a program accesses a page that is not currently loaded in physical memory. The OS pauses the program, checks the access is legitimate, loads the missing page from disk into a free frame, updates the page table and resumes the program. Under demand paging this is a normal, expected event.

Canonical: https://devsunite.com/resources/core-subjects/operating-systems/page-fault-handling
Last updated: 2026-11-22

Subject: Operating Systems · Category: Memory Management · 4 min read

A page fault is a hardware-generated exception (a trap raised by the CPU's memory management unit, not an external interrupt) that fires when a running program tries to access a virtual memory page that isn't currently mapped to a physical frame. Rather than being a crash or an error in the everyday sense, this is a completely normal, expected event under demand paging, it's exactly the mechanism that lets pages be loaded lazily instead of all at once.

## Step-by-step: what happens on a page fault

1. The CPU generates a logical address as part of a normal instruction, and the MMU checks the page table entry for that page.
2. The page table entry has a "valid/invalid" bit; if it's marked invalid (the page isn't currently in physical memory), the hardware raises a page fault, trapping into the operating system.
3. The OS checks whether the access was actually legitimate (a real page in the process's valid address space that just isn't loaded right now) or truly illegal (accessing memory the process was never allowed to touch at all). A legitimate-but-not-loaded access continues; an illegal one results in the process being terminated (this is the real mechanism behind a segmentation fault crash).
4. For a legitimate fault, the OS finds a free physical frame. If none is free, a page replacement algorithm (its own dedicated topic) picks an existing page to evict, writing it back to disk first if it's been modified.
5. The OS reads the needed page's actual data from disk (from the file system, for code, or from swap space, for previously-swapped-out data) into the free frame.
6. The page table is updated: the faulting page's entry is marked valid and pointed at the new frame.
7. The instruction that originally caused the fault is restarted from scratch, and this time the page table lookup succeeds, so execution proceeds normally.

## A worked example

A process accesses logical address in page 7, but page 7's page table entry is marked invalid. Page fault triggers. The OS checks: page 7 is within this process's legitimate address range, so it's a real, valid fault, not an illegal access. No free frame is currently available, so the OS runs its page replacement algorithm and picks page 3 (currently in frame 12) to evict. Page 3 has been modified since it was loaded (its dirty bit is set), so the OS first writes page 3's contents back to disk, then reads page 7's actual contents into frame 12, updates the page table so page 7 now maps to frame 12 and page 3 is marked invalid, and finally restarts the original instruction, which now succeeds.

## Where this matters in real systems

Page faults happen constantly, even in a healthy running system, every time a program touches a part of its memory for the first time or a page that had been swapped out. What matters for real performance is the fault rate: a system that's faulting excessively (called thrashing, when the system spends more time handling faults than doing real work) is a genuine, diagnosable performance problem, and tools like Linux's `vmstat` directly report page fault rates for exactly this reason.

## What interviewers actually ask about page fault handling

- **"What is a page fault?"** A hardware trap that occurs when a program accesses a virtual memory page not currently mapped to a physical frame.
- **"Walk me through the full sequence of steps when a page fault occurs."** Hardware detects invalid page table entry, traps to OS, OS validates the access, finds or frees a physical frame (running page replacement if needed, writing back a dirty page first), loads the needed page from disk, updates the page table, restarts the faulting instruction.
- **"What's the difference between a legitimate page fault and an illegal memory access?"** A legitimate fault is for a page within the process's real address space that just isn't currently loaded; an illegal access is the process trying to touch memory it was never allowed to access at all, and results in termination rather than a page load.
- **"What is thrashing and how does it relate to page faults?"** Thrashing is when a system spends more time handling page faults than doing actual useful work, typically because too little physical memory is available relative to what's actively needed.

## Key takeaways

- A page fault is a hardware trap for accessing a virtual page not currently in a physical frame.
- The OS validates the access, finds/frees a frame (possibly evicting and writing back another page), loads the needed page, updates the page table, and restarts the instruction.
- Not every page fault is illegal; most under demand paging are expected and normal.
- Excessive faulting relative to available memory is called thrashing, a real, diagnosable performance problem.

## Related

- [Previous: Virtual Memory and Demand Paging](https://devsunite.com/resources/core-subjects/operating-systems/virtual-memory-and-demand-paging)
- [Next: Page Replacement Algorithms](https://devsunite.com/resources/core-subjects/operating-systems/page-replacement-algorithms)
- [More in Memory Management: Logical vs Physical Address Space](https://devsunite.com/resources/core-subjects/operating-systems/logical-vs-physical-address-space)
- [More in Memory Management: Swapping and Contiguous Memory Allocation](https://devsunite.com/resources/core-subjects/operating-systems/swapping-and-contiguous-memory-allocation)
- [More in Memory Management: Fragmentation in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/fragmentation-in-os)
- [More in Memory Management: Paging in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/paging-in-os)
- [More in Memory Management: Segmentation in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/segmentation-in-os)
- [All 205 OS interview questions](https://devsunite.com/resources/core-subjects/operating-systems)
