Introduction to Page Fault (Including Types and Impact) [MiniTool Wiki]
What Is a Page Fault?
Although the term “page fault” sounds like an error, it is a common and necessary condition for increasing the amount of memory available to programs in any operating system that uses virtual memory including OpenVMS, Microsoft Windows, Unix-like systems (including macOS, Linux, *BSD, Solaris, AIX, and HP-UX), and z/OS.
In programming terms, a page fault produces an exception that informs the operating system that it must retrieve memory blocks or “pages” from virtual memory in order for the program to continue. Once the data is moved to physical memory, the program will continue to run normally. This process takes place in the background and is usually not noticed by the user.
Types of Page Fault
After getting some information about the page fault, this part you can know the three different types of a page fault.
Minor Page Fault
If a page is loaded into memory when a fault occurs, but the page is not marked as being loaded into memory in the memory management unit, then the page is called a minor page fault or soft page fault.
The page fault handler in the operating system only needs to make the entry of the page in the memory management unit point to the page in the memory and indicate that the page has been loaded into the memory; it does not need to read the page into the memory. This can happen if the memory is shared by different programs and the page has been brought into the memory of other programs.
Major Page Fault
A major page fault is a mechanism used by the operating system to increase the amount of available program memory on demand. The operating system will delay loading certain parts of the program from disk until the program tries to use it and generates a page fault.
If the page is not loaded into memory when the fault occurs, it is called a major or hard page fault. The page fault handler in the operating system needs to find a free location: a free page in memory or a non-free page in memory. The latter may be used by another process, in which case the operating system needs to write out the data in that page (if the data has not been written out since the last modification), and mark that page as not being loaded in memory in its process page table.
Once the space is available, the operating system can read the data of the new page into the memory, add an entry to its location in the memory management unit, and indicate that the page has been loaded. Therefore, major faults are more expensive than minor faults and increase the memory access latency of interrupted program execution.
Invalid Page Fault
If a page fault occurs to reference an address that does not belong to the virtual address space, which means that there is no corresponding page in the memory, it is called an invalid page fault. Then, the page fault handler in the operating system usually passes a segmentation fault to the offending process, indicating that the access was invalid; this usually causes the invalid referenced code to terminate abnormally.
A null pointer is often represented as a pointer to address 0 in the address space; many operating systems set the MMU to indicate that the page containing the address is not in memory, and does not contain the page in the virtual address space, so to try to read or write, the memory referenced by the null pointer is invalid.
Essentially, page faults will reduce the performance of the program or operating system, and in the case of degradation, may cause a crash. Optimization of programs and operating systems that reduce the number of page faults can improve the performance of programs and even the entire system.
The two main focuses are to reduce overall memory usage and improve memory locality. To reduce page faults in the system, the programmer must use an appropriate page replacement algorithm that fits the current requirements and maximizes the page hit rate.