Prerequisite : Page Replacement Algorithms
In operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce number of page faults.
First In First Out (FIFO) page replacement algorithm –
This is the simplest page replacement algorithm. In this algorithm, operating system keeps track of all pages in the memory in a queue, oldest page is in the front of the queue. When a page needs to be replaced page in the front of the queue is selected for removal.
Example -1. Consider page reference string 1, 3, 0, 3, 5, 6 and 3 page slots.
Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1 Page Fault.
Finally 6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault.
So total page faults = 5.
Example -2. Consider the following reference string: 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1.
Using FIFO page replacement algorithm –
So, total number of page faults = 9.
Given memory capacity (as number of pages it can hold) and a string representing pages to be referred, write a function to find number of page faults.
Implementation – Let capacity be the number of pages that memory can hold. Let set be the current set of pages in memory.
1- Start traversing the pages. i) If set holds less pages than capacity. a) Insert page into the set one by one until the size of set reaches capacity or all page requests are processed. b) Simultaneously maintain the pages in the queue to perform FIFO. c) Increment page fault ii) Else If current page is present in set, do nothing. Else a) Remove the first page from the queue as it was the first to be entered in the memory b) Replace the first page in the queue with the current page in the string. c) Store current page in the queue. d) Increment page faults. 2. Return page faults.
Note – We can also find the number of page hits. Just have to maintain a separate count. If the current page is already in the memory then that must be count as Page-hit.
Belady’s anomaly –
Belady’s anomaly proves that it is possible to have more page faults when increasing the number of page frames while using the First in First Out (FIFO) page replacement algorithm. For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10 page faults.
This article is contributed by Sahil Chhabra. 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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- FIFO (First-In-First-Out) approach in Programming
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Find the largest multiple of 3 | Set 1 (Using Queue)
- Intuit Interview Experience | Set 7 (For 3.5 Years Experienced)
- Accolite Interview Experience | Set 3 (On-Campus)
- MAQ Software Interview Experience | Set 10 (On-Campus)
- Walmart Labs Interview Experience | Set 3 (On-Campus)
- MakeMyTrip Interview Experience | Set 9 (Off-Campus for Sr. Android developer))
- Walmart Labs Interview Experience | Set 5 (On-Campus)
- Microsoft Interview Experience | Set 76 (On-Campus)
- Walmart Lab Interview Experience | Set 8 (Off-Campus 3 Years Experience)
- MakeMyTrip Interview Experience | Set 11 (Developer Position)
- Minimum steps to reach target by a Knight | Set 1
- Circular Queue | Set 1 (Introduction and Array Implementation)
- Paytm Interview Experience | Set 12 (For 1.5 Years Experienced)
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Queue | Set 1 (Introduction and Array Implementation)
- Priority Queue | Set 1 (Introduction)
- Deque | Set 1 (Introduction and Applications)