Each page has been assigned a frame in the main memory and when a thread attempts to make a reference to a page whose frame is not found in the main memory, a page fault occurs. If the page is truly not present in the physical memory and it has to be retrieved from secondary storage, such as a hard disk, the fault occurs which is classified as a hard page fault.
What is Page Fault?
A page fault is a type of exception initiated by the hardware of a computer to notify the operating system of references to frameless pages. Simply put, a page fault occurs when a program tries to access the data residing in a page that is not stored in the main memory or physical memory. Let’s say a particular program has five pages out of which two pages are loaded into the main memory into two separate frames. So, when a new page is called by the CPU, one of few things may occur. If that page is not found in the main memory, it’s called a page fault. So, when the CPU demands a particular page to be executed, but the page is not available in the main memory, page fault occurs. The page actually resides in the secondary memory or virtual memory. This whole mechanism is handled by the memory management unit (MMU) hardware.
What is Hard Page Fault?
If the desired page is not present in the main memory and it has to be fetched or paged in from the virtual memory, the fault is classified as a ‘hard’ page fault. Each page table entry has a validity bit, which indicates if the page has been assigned a frame in the main memory. So when a program makes a reference to a page whose validity bit is zero, page fault occurs. The OS then intervenes and assigns a suitable frame to the page. The page table entry actually tells us the page is on disk. So, when a desired page is not found in the main memory and it is swapped out to the page file on the disk instead, hard page fault occurs. The OS has to retrieve the page from the disk subsystem which causes the program to wait significantly, which in turn reduces performance. If the page resides in the cache memory, a soft page fault occurs.
Difference between Page Fault and Hard Fault
Exception
– A page fault is a type of exception that occurs when a program tries to access a page that is in its address space, but is not found in the main memory or physical memory. So, when the CPU demands a particular page to be executed, but the page is not available in the main memory, page fault occurs. If the desired page is not present in the main memory and it has to be fetched or paged in from the virtual memory, the fault is classified as a ‘hard’ page fault. Simply put, if the page has to be retrieved from the disk, a hard page fault occurs.
Handling
– The memory management unit (MMU) signals a page fault to the CPU which then raises a page fault exception, which in turn prompts the operating system to locate the page in the main memory. The OS then assigns a suitable frame to the page. This action is managed by a module called ‘pagefault handler.’ The page table is then updated and the page entry should point to the right frame by now. The program is then ready to run again, but this time without a page fault. When a hard page fault occurs, the OS finds a free location for the page and once the space is available, the OS reads the page data into main memory, and updates the entry in the MMU.
Page Fault vs. Hard Page Fault: Comparison Chart
Summary
Page fault is one of the common memory management problems when dealing with virtual memory or secondary memory. A page fault, on the contrary to what its name suggests, is not actually a fault; it is an exception that occurs when a program attempts to access an address on a page which is not available in the main memory where it is supposed to be located. The MMU then signals the CPU of the page fault and the CPU then prompts the OS to locate the page in memory while the thread goes into waiting. So, the OS then locates the desired page in another part of the physical memory or in worst case, finds it on the disk, in which case, the fault is called a hard page fault. The OS then assigns a suitable frame to the page and updates the page table. This whole mechanism is handled by a module called the pagefault handler.