Page Fault in Operating System

In this tutorial, we will be covering the concept of page fault and how to handle page fault in the Operating System.

Page Fault in OS

Page fault dominates more like an error. It mainly occurs when any program tries to access the data or the code that is in the address space of the program, but that data is not currently located in the RAM of the system.

  • So basically when the page referenced by the CPU is not found in the main memory then the situation is termed as Page Fault.

  • Whenever any page fault occurs, then the required page has to be fetched from the secondary memory into the main memory.

In case if the required page is not loaded into the memory, then a page fault trap arises

The page fault mainly generates an exception, which is used to notify the operating system that it must have to retrieve the "pages" from the virtual memory in order to continue the execution. Once all the data is moved into the physical memory the program continues its execution normally. The Page fault process takes place in the background and thus goes unnoticed by the user.

  • The hardware of the computer tracks to the kernel and the program counter (PC) is generally saved on the stack.CPU registers store the information of the current state of instruction.

  • An assembly program is started that usually saves the general registers and also saves the other volatile information to prevent the OS from destroying it.

Handling the Page Fault

Given below is the simple procedure to handle the page fault:

Figure: Steps to Handle the Page fault

If you will access a page that is marked as invalid then it also causes a Page Fault. Then the Paging hardware during translating the address through the page table will notice that the invalid bit is set that will cause a trap to the Operating system.

This trap is mainly the result of the failure of the Operating system in order to bring the desired page into memory.

Let us understand the procedure to handle the page fault as shown with the help of the above diagram:

  1. First of all, internal table(that is usually the process control block) for this process in order to determine whether the reference was valid or invalid memory access.
  2. If the reference is invalid, then we will terminate the process. If the reference is valid, but we have not bought in that page so now we just page it in.
  3. Then we locate the free frame list in order to find the free frame.
  4. Now a disk operation is scheduled in order to read the desired page into the newly allocated frame.
  5. When the disk is completely read, then the internal table is modified that is kept with the process, and the page table that mainly indicates the page is now in memory.
  6. Now we will restart the instruction that was interrupted due to the trap. Now the process can access the page as though it had always been in memory.