What is a Page Fault?
A page fault is an interrupt (or exception) raised by the Memory Management Unit (MMU) when a program tries to access a memory page that is not currently loaded in physical memory (RAM). In other words, it occurs when a process attempts to access data or code that resides in virtual memory but is not currently present in real (physical) memory.
Page faults are a normal and essential part of demand paging, which is used by most modern operating systems to implement virtual memory. Not all pages of a process need to be in RAM at once; only those needed are loaded on demand.
Types of Page Faults:
-
Major (Hard) Page Fault:
- The required page is not in RAM and must be loaded from disk (e.g., from swap space or the file system).
- This causes disk I/O and is relatively slow.
-
Minor (Soft) Page Fault:
- The page is already in RAM but not mapped in the current process's page tables.
- For example, the page may have been paged in by another process or temporarily unmapped.
- No disk I/O is needed; just an update to the page tables.
-
Invalid Page Fault (Protection Fault):
- The process tried to access a memory address that is protected or invalid (e.g., writing to read-only memory).
- This can result in a segmentation fault or access violation if not handled properly.
How Does the Operating System Handle a Page Fault?
When a page fault occurs, the hardware (CPU) triggers an interrupt, and the page-fault handler in the OS kernel takes control. Here’s how the OS typically handles a page fault:
1. Interrupt Handling:
- The CPU detects that a page fault has occurred and saves the current state (registers, instruction pointer, etc.).
- It transfers control to the OS’s page-fault exception handler.
2. Determine the Cause:
- The OS checks:
- Whether the virtual address accessed is valid for the process.
- Whether the access was a read or write.
- Whether the access violates protection rules.
3. Valid Page Access?
- If the virtual address is invalid, the OS sends a segmentation fault signal (e.g.,
SIGSEGV on Unix-like systems), and the process is usually terminated.
- If the address is valid, but the page is not in RAM, proceed to load it.
4. Find a Free Frame:
- The OS looks for a free frame in physical memory.
- If no free frames are available, it uses a page replacement algorithm (like LRU – Least Recently Used) to evict a page from RAM.
5. Load the Required Page:
- The OS reads the required page from disk (swap space or executable file) into the selected frame in RAM.
6. Update Page Tables:
- The OS updates the process’s page table entry to map the virtual page to the newly loaded physical frame.
7. Resume Execution:
- The OS restores the saved state of the process and restarts the instruction that caused the page fault.
- The instruction will now execute successfully since the required data is now in memory.
Example Scenario:
Suppose a process starts executing and accesses a variable located in a memory page that hasn’t been loaded into RAM yet.
- A page fault is triggered.
- The OS determines the page is valid but not in RAM.
- It loads the page from disk into a free frame.
- It updates the page table to point to this new frame.
- The process resumes execution as if nothing happened.
Summary:
| Aspect | Description |
|---|
| Definition | A page fault occurs when a program accesses a memory page not currently in physical memory. |
| Purpose | Enables demand paging and efficient use of virtual memory. |
| Handling Steps | Interrupt → Check validity → Load page from disk → Update page tables → Resume execution |
| Performance Impact | Major page faults are slow due to disk I/O; minor ones are fast. |
| Common Cause | Starting a large program, accessing rarely-used data, or insufficient RAM. |
In short, a page fault is a mechanism that allows the OS to transparently manage memory by loading pages on demand while maintaining the illusion of a large, contiguous address space for each process.