Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Page Fault Handling in Operating System

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

A Page Fault is an exception that occurs by the Memory Management Unit (MMU) when a program tries to access memory that hasn't been prepared.

Page Fault Handling in Operating Systems is important because the Operating System will step in to efficiently manage page retrieval and allocation, which will ensure the smooth execution of the program.

Page Fault Handling in Operating System

As we know, operating system refers to a task as a Page. A missing page in the Page table(where the page’s address is stored) is known as Page Fault.

What is Page Fault in Operating System?

When the page accessed by the CPU is not found in the main memory, the situation is referred to as Page Fault. If the required page is not loaded into the memory, then a page fault trap arises.

To recover from this, the required page has to be fetched from the secondary memory(hard disk) into the main memory.

The page fault primarily causes an exception, which informs the operating system that it will need to retrieve "pages" from virtual memory in order to continue the execution. The software resumes normal operation once all of the data has been placed into physical memory. The Page Fault procedure happens in the background, so the user isn't aware of it. The computer's hardware is connected to the kernel, and the program counter (PC) is usually saved on the stack. The information about the current status of instruction is stored in CPU registers. An assembly program is begun, which preserves the general registers as well as other volatile data to prevent the OS from destroying it.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Describe the Actions Taken by the Operating System When a Page Fault Occurs.

When a page fault occurs in an operating system, it means that the requested memory page isn't currently in physical RAM but rather resides in secondary storage, like a hard drive or SSD. Here are the actions taken by the OS:

  • Page Fault Trap: The CPU detects a page fault while trying to access a memory page that isn't currently in RAM. This triggers a page fault exception, which transfers control to the operating system.
  • Page Table Lookup: The operating system looks up the page table to determine the location of the required page in secondary storage, such as a disk.
  • Fetch Page from Secondary Storage: The OS initiates a process to fetch the required page from secondary storage into an available page frame in RAM. This involves reading the page data from disk into a free page frame.
  • Update Page Table: Once the page is successfully brought into RAM, the operating system updates the page table to reflect the new location of the page in physical memory. This includes updating the page table entry with the physical address of the page frame where the page now resides.
  • Resume Process Execution: Finally, the operating system restarts the interrupted memory access instruction that caused the page fault. With the required page now available in RAM, the process can proceed as expected, accessing the data it needs from memory.

Page Fault Handling

The below image explains how Page Fault Handling will be done by the operating system.

Page Fault Handling

A Page Fault occurs when you access a page that has been flagged as invalid. Then, while translating the address across the page table, the paging hardware will notice that the incorrect bit is set, triggering a trap in the operating system.

This trap is mostly caused by the Operating System's failure to put the desired page into memory.

Let's look at the technique for dealing with a page fault, as depicted in the diagram above:

  • To begin, an internal table (typically the process control block) was created for this process to identify whether the reference was valid or invalid memory access.
  • If the reference is invalid, the process will be terminated. If the reference is correct, but we haven't yet purchased that page, we'll just page it in.
  • Then, in order to find the free frame, we locate the free frame list.
  • In order to read the required page into the newly allocated frame, a disc operation is now scheduled.
  • When the disc is entirely read, the internal table that is kept with the process is amended, and the page table, which primarily shows that the page is now in memory, is updated.
  • We'll now restart the instruction that was interrupted due to the trap. The page can now be accessed as if it had always been in memory by the process.

Types of page faults

There are a total of 3 types of page faults as follows.

Minor Page Fault 

A small page fault or soft page fault happens when a page is loaded into memory when a fault occurs, but the page is not registered as being loaded into memory in the memory management unit.

Major Page Fault 

The operating system uses a significant page fault to boost available program memory on demand. Until the program tries to use it and creates a page fault, the operating system will postpone loading some program parts from the disc.

Invalid Page Fault 

An invalid page fault happens when a page fault references an address that does not belong to the virtual address space, implying no equivalent page in memory.

Page Fault Terminology

  • Page Hit: It occurs when a program references a memory page that is already present in the main physical memory (RAM). This leads to quick retrieval of the required data without the need for additional disk access
     
  • Page Miss: It happens when a program accesses a memory page that is not currently residing in the main memory. This triggers a page fault and requires the OS to bring the missing page into RAM from secondary storage
     
  • Page Fault Time: It refers to the total time taken to handle a page fault. It includes the time to find the required page on disk, swap it into RAM, update data structures, and restart the interrupted program
     
  • Page Fault Delay: This term denotes the time interval between the moment a page fault occurs and the time when the corresponding page is fully loaded into the main memory, allowing the program to continue its execution
     
  • Hard Page Fault: It takes place when the missing page is not present in any available storage, including both RAM and secondary storage. This situation often requires complex memory management techniques, such as swapping pages to disk
     
  • Soft Page Fault: It occurs when the required page is found elsewhere in memory or storage but hasn't been assigned to the correct address space of the requesting process. It's generally easier to resolve than a hard page fault
     
  • Minor Page Fault: It signifies that the page being accessed is already in memory, but it is marked as not yet loaded. It typically happens when a program first accesses a memory page, causing the OS to load it into the main memory for future use.

Frequently Asked Questions

What is buffering in OS?

Buffering in operating systems refers to temporarily storing data in a buffer memory area before it's processed or transferred. It helps optimize input/output operations by reducing the frequency of data transfers between slow and fast devices, enhancing system performance.

How page fault occur in the operating system?

A page fault occurs in the operating system when the page accessed by the CPU is not found in the main memory. If the required page is not loaded into the memory, then a page fault trap arises. To solve that, we fetch the required page from secondary memory.

What are the different types of page fault?

When the page accessed by the CPU is not found in the main memory, the situation is referred to as a Page Fault. There are three types of page faults: Minor Page Fault, Major Page Fault, and Invalid Page Fault. 

What is an example of a page fault?

An example of page fault is when a program requests data from a virtual memory page that has been swapped out to disk, which causes the operating system to retrieve it from storage and load it back into RAM to fulfil the request.

Conclusion

In this article, we have discussed about Page Fault Handling in Operating System. We have discussed what it is and how it occurs. Understanding page fault handling is crucial for comprehending memory management in operating systems. Page faults optimize memory usage by efficiently managing data between RAM and secondary storage.

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!


Also, check out some other topics such as,Operating Systems, Computer Networks, DBMS, etc. as well as some Contests and Test Series only on Coding Ninjas Studio. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations. 

Topics covered
1.
Introduction
2.
What is Page Fault in Operating System?
3.
Describe the Actions Taken by the Operating System When a Page Fault Occurs.
4.
Page Fault Handling
5.
Types of page faults
5.1.
Minor Page Fault 
5.2.
Major Page Fault 
5.3.
Invalid Page Fault 
6.
Page Fault Terminology
7.
Frequently Asked Questions
7.1.
What is buffering in OS?
7.2.
How page fault occur in the operating system?
7.3.
What are the different types of page fault?
7.4.
What is an example of a page fault?
8.
Conclusion