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 firstname.lastname@example.org. 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.
- Program for Least Recently Used (LRU) Page Replacement algorithm
- Optimal Page Replacement Algorithm
- Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
- FIFO (First-In-First-Out) approach in Programming
- Minimize Cost with Replacement with other allowed
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
- Program for Shortest Job First (SJF) scheduling | Set 2 (Preemptive)
- Top 20 Greedy Algorithms Interview Questions
- Correctness of Greedy Algorithms
- Greedy Algorithms (General Structure and Applications)
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Write a program to print all permutations of a given string
- Program for array rotation
- Program for Next Fit algorithm in Memory Management
- Longest Remaining Time First (LRTF) CPU Scheduling Program
- Python Program to check if given array is Monotonic