A page fault occurs when a program attempts to access data or code that is in its address space, but is not currently located in the system RAM. So when page fault occurs then following sequence of events happens :
- The computer hardware traps to the kernel and program counter (PC) is saved on the stack. Current instruction state information is saved in CPU registers.
- An assembly program is started to save the general registers and other volatile information to keep the OS from destroying it.
- Operating system finds that a page fault has occurred and tries to find out which virtual page is needed. Some times hardware register contains this required information. If not, the operating system must retrieve PC, fetch instruction and find out what it was doing when the fault occurred.
- Once virtual address caused page fault is known, system checks to see if address is valid and checks if there is no protection access problem.
- If the virtual address is valid, the system checks to see if a page frame is free. If no frames are free, the page replacement algorithm is run to remove a page.
- If frame selected is dirty, page is scheduled for transfer to disk, context switch takes place, fault process is suspended and another process is made to run until disk transfer is completed.
- As soon as page frame is clean, operating system looks up disk address where needed page is, schedules disk operation to bring it in.
- When disk interrupt indicates page has arrived, page tables are updated to reflect its position, and frame marked as being in normal state.
- Faulting instruction is backed up to state it had when it began and PC is reset. Faulting is scheduled, operating system returns to routine that called it.
- Assembly Routine reloads register and other state information, returns to user space to continue execution.
This article is contributed by Swasthik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Inverted Page Table in Operating System
- Hashed Page Tables in Operating System
- Page Table Entries in Page Table
- System Protection in Operating System
- User View Vs Hardware View Vs System View of Operating System
- System Programs in Operating System
- File System Implementation in Operating System
- Xv6 Operating System -adding a new system call
- Traps and System Calls in Operating System (OS)
- Page Replacement Algorithms in Operating Systems
- Process Schedulers in Operating System
- Introduction of Deadlock in Operating System
- Thread in Operating System
- Paging in Operating System
- Banker's Algorithm in Operating System
- Segmentation in Operating System
- File Systems in Operating System
- Virtual Memory in Operating System
- Real Time Operating System (RTOS)
- Remote Procedure Call (RPC) in Operating System