A Page Fault is a type of interrupt (specifically a trap) generated by the Memory Management Unit (MMU) when a process attempts to access a virtual page that is not currently loaded into any physical memory frame. Sometimes a program tries to access a page that is not currently in physical memory, when this happens, a Page Fault occurs.
Page Fault HandlingThis mechanism is the foundation of Virtual Memory, allowing data to be brought into RAM only when it is actually needed — a concept known as Demand Paging. When the CPU requests data from a memory page, the MMU checks whether that page is in RAM.
- If it is present → normal access continues.
- If it is absent → the MMU triggers a Page Fault interrupt to the operating system.
Steps in Page Fault Handling
The process of handling a page fault involves several systematic steps. Each step ensures that the required page is correctly loaded into memory and that the CPU can resume the interrupted process without errors.
1. Trap to the Kernel
When a page fault occurs, the hardware triggers a trap to the OS. The CPU switches from user mode to kernel mode, and the current instruction state — including the Program Counter (PC), registers, and other processor states — is saved.
2. Validate Access
The OS first validates the access attempt. It checks if the requested virtual address is a legitimate one belonging to the process’s address space.
- If the address is invalid (e.g., accessing unallocated or protected memory), the OS raises a segmentation fault or access violation, and the process is terminated.
- If valid, the OS proceeds to locate the page on disk.
3. Locate the Page on Disk
Next, the OS determines where the required page is stored on the secondary storage. This could be in the swap area, a page file, or the process’s executable file.
4. Allocate a Free Frame
Now, the OS needs a free physical frame in RAM to hold the page.
- If a free frame is available, it is assigned directly.
- If no free frame is available, the OS executes a Page Replacement Algorithm. The algorithm selects a victim frame — a page currently in RAM that can be swapped out to make space.
If the chosen victim page has been modified (the Dirty Bit is set), it must first be written back to disk before being replaced.
Once a frame is ready, the OS initiates a disk read operation to load the required page from disk into the allocated frame in RAM. This is the slowest part of the page fault handling process since disk access time is significantly higher than RAM access time.
6. Update the Page Table
After the page has been loaded into RAM:
- The Page Table Entry (PTE) for the page is updated.
- The Frame Number field is filled with the physical frame number.
- The Present Bit is set to
1 to indicate that the page is now in memory.
7. Restart the Instruction
Finally, the OS restores the saved CPU state and restarts the instruction that caused the fault. This time, the address translation succeeds because the page is now in memory, and the program continues as if the fault never occurred..
Types of Page Faults
Following are three types of page faults with examples:
1. Minor Page Fault
A minor page fault happens when the page, the program needs, is already in RAM, but the process’s page table doesn’t have the correct mapping yet. In other words, the data is available, but the OS just needs to “connect the dots” so the program can access it.
Example:
Imagine you have a notebook on your desk (RAM), but your friend (process) doesn’t know which page has the information. You quickly point it out, and they can read it immediately. Since the data is already in memory, this happens almost instantly, with no delay — this is the “invisible fix.”
Key point: Fast to resolve; no disk access needed.
2. Major Page Fault
A major page fault occurs when the page, the program needs, is not in RAM at all. The OS must fetch it from the hard disk or SSD, which is much slower. This can noticeably delay the program.
Example:
Imagine you want to read a book that’s stored in a library (disk) rather than on your desk (RAM). You have to walk to the library, find the book, bring it back, and then continue reading. That’s a major page fault — the program literally waits for the data to arrive.
Key point: Time-consuming; disk I/O is required; performance impact is noticeable.
3. Invalid Page Fault
An invalid page fault occurs when the program tries to access a memory address it shouldn’t, such as one that’s protected, unallocated, or beyond its limits. This is usually a programming error or a bug.
Example:
It’s like trying to open a locked door in someone else’s house — you’re not allowed to go there. The OS stops the program and often triggers a crash or error message, like “Segmentation Fault.”
Key point: Indicates a serious error; usually terminates the program.
Impact of Page Faults on System Performance
While page faults are a normal part of virtual memory management, excessive page faults can harm system performance.
- Increased Latency: Accessing pages from disk takes much longer than from RAM.
- Reduced CPU Utilization: The CPU often remains idle while waiting for disk I/O operations to complete.
- Thrashing: When too many page faults occur in a short period, the system spends more time swapping pages than executing processes. This state severely degrades overall performance.
To prevent thrashing, operating systems use techniques like Working Set Models and Page Fault Frequency Control to balance memory usage.