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.
- Program for Page Replacement Algorithms | Set 1 ( LRU)
- Program for Optimal Page Replacement Algorithm
- FIFO (First-In-First-Out) approach in Programming
- Minimize Cost with Replacement with other allowed
- Correctness of Greedy Algorithms
- Top 20 Greedy Algorithms Interview Questions
- Program for array rotation
- Program for Shortest Job First (SJF) scheduling | Set 2 (Preemptive)
- Program for Shortest Job First (or SJF) scheduling | Set 1 (Non- preemptive)
- Program for First Fit algorithm in Memory Management
- Program for Best Fit algorithm in Memory Management
- Write a program to print all permutations of a given string
- Python Program to check if given array is Monotonic
- Program for Worst Fit algorithm in Memory Management
- Operating System | Program for Next Fit algorithm in Memory Management
Improved By : Villan