A page fault is an exception that occurs when a process attempts to access a memory page that is not currently stored in the systems RAM. It is raised by the hardware and trapped by the operating system. A page fault can be categorized as a minor or soft page fault, or a major or hard page fault.
-
Minor or Soft Page Fault: This occurs when the page is loaded in memory at the time the fault is generated, but is not marked in the memory management unit as being loaded in memory. The page fault handler in the operating system merely needs to make the entry for that page in the memory management unit point to the page in memory and indicate that the page is loaded in memory. It does not need to read the page into memory.
-
Major or Hard Page Fault: This occurs when the page is not loaded in memory at the time of the fault. The page fault handler in the operating system needs to find a free location, either a free page in memory or a non-free page in memory. If a page frame is not free, the page replacement algorithm is run to remove a page. If the frame selected is dirty, the page is scheduled for transfer to disk, and a context switch takes place. The fault process is suspended, and another process is made to run until the disk transfer is completed. As soon as the page frame is clean, the operating system looks up the disk address where the needed page is and schedules a disk operation to bring it in. When the disk interrupt indicates that the page has arrived, the page tables are updated to reflect its position, and the frame is marked as being in a normal state. The faulting instruction is backed up to the state it had when it began, and the program counter is reset. The faulting is scheduled, and the operating system returns to the routine that called it.
If a page fault occurs for a reference to an address that is not part of the virtual address space, meaning there cannot be a page in memory corresponding to it, then it is called an invalid page fault. The page fault handler in the operating system will then generally pass a segmentation fault to the offending process, indicating that the access was invalid. This can cause the program to crash.
Page fault handling is typically automated, where the operating systems kernel allocates or denies RAM access to specific processes. Page faults are common and often helpful to improve performance by raising the quantum of memory available for programs.