Page Faults and Demand Paging
What you'll learn: How the OS handles accessing memory pages that aren't currently loaded in physical RAM.
What is a Page Fault?
A page fault occurs when a program tries to access a virtual memory page that isn't currently mapped to physical memory. Think of it like looking for a book on your desk, only to realize it's still on the bookshelf across the room—you have to walk over and fetch it before you can read it.
When the Memory Management Unit (MMU) tries to translate a virtual address using the page table, it discovers the page isn't present. This triggers an interrupt that hands control to the operating system's page fault handler.
The Demand Paging Process
Demand paging is the strategy of loading pages into physical memory only when they're needed, rather than loading an entire program at startup.
Here's what happens step-by-step:
- Fault occurs: Program accesses a page not in RAM
- OS takes control: The page fault handler is invoked
- Find the page: OS locates the page on disk (usually in swap space)
- Load into RAM: OS reads the page from disk into a free physical frame
- Update page table: The page table entry is updated with the new physical address and marked as "present"
- Resume execution: The faulting instruction is restarted, now succeeding
This approach saves memory by keeping only actively-used pages in RAM. The trade-off? Disk access is thousands of times slower than RAM access, making page faults expensive.
Key Takeaway: A page fault is a hardware exception that occurs when accessing a non-resident page, triggering the OS to load it from disk via demand paging—enabling programs to use more memory than physically available at the cost of occasional disk I/O delays.