A page fault occurs when a program tries to access a page of memory that is not currently in the system's physical memory (RAM). This can happen when the data or code the program needs is in its address space but has not been loaded into RAM yet. The operating system then steps in to retrieve the required page from secondary storage (like a hard disk) and load it into memory so the program can continue executing.
Causes of Page Faults
- Accessing a page not currently loaded in RAM due to demand paging.
- Accessing invalid memory outside allowed bounds.
- Violating protection rules, such as writing to a read-only page.
Types of Page Faults
- Minor Page Fault: The required page is in memory but not marked as present in the page table.
- Major Page Fault: The page is not in RAM and must be fetched from disk.
- Invalid Page Fault: The accessed memory address is invalid, leading to process termination.
Page faults are handled by the operating system through saving the processor state, validating the memory access, loading the page into RAM if valid, updating page tables, and then resuming the program. Frequent page faults can degrade system performance and may lead to thrashing, where the system spends more time handling page faults than executing processes.