Belady’s Anomaly in Page Replacement Algorithms

06 May, 2022
Prerequisite – Page Replacement Algorithms In Operating System, process data is loaded in fixed-sized chunks and each chunk is referred to as a page. The processor loads these pages in the fixed-sized chunks of memory called frames. Typically the size of each page is always equal to the frame size. 
A page fault occurs when a page is not found in the memory and needs to be loaded from the disk. If a page fault occurs and all memory frames have been already allocated, then replacement of a page in memory is required on the request of a new page. This is referred to as demand-paging. The choice of which page to replace is specified by page replacement algorithms. The commonly used page replacement algorithms are FIFO, LRU, optimal page replacement algorithms, etc. 
Generally, on increasing the number of frames to a process’ virtual memory, its execution becomes faster as fewer page faults occur. Sometimes the reverse happens, i.e. more page faults occur when more frames are allocated to a process. This most unexpected result is termed Belady’s Anomaly. 
Bélády’s anomaly is the name given to the phenomenon where increasing the number of page frames results in an increase in the number of page faults for a given memory access pattern. 
This phenomenon is commonly experienced in the following page replacement algorithms: 
First in first out (FIFO) Second chance algorithm Random page replacement algorithm 
First in first out (FIFO) 
Second chance algorithm 
Random page replacement algorithm 
Reason for Belady’s Anomaly – The other two commonly used page replacement algorithms are Optimal and LRU, but Belady’s Anomaly can never occur in these algorithms for any reference string as they belong to a class of stack-based page replacement algorithms. 
A stack-based algorithm is one for which it can be shown that the set of pages in memory for N frames is always a subset of the set of pages that would be in memory with N + 1 frames. For LRU replacement, the set of pages in memory would be the n most recently referenced pages. If the number of frames increases then these n pages will still be the most recently referenced and so, will still be in the memory. While in FIFO, if a page named b came into physical memory before a page – a then priority of replacement of b is greater than that of a, but this is not independent of the number of page frames and hence, FIFO does not follow a stack page replacement policy and therefore suffers Belady’s Anomaly. 
Example: Consider the following diagram to understand the behavior of a stack-based page replacement algorithm 
The diagram illustrates that given the set of pages i.e. {0, 1, 2} in 3 frames of memory is not a subset of the pages in memory – {0, 1, 4, 5} with 4 frames and it is a violation in the property of stack based algorithms. This situation can be frequently seen in FIFO algorithm. 
Belady’s Anomaly in FIFO – Assuming a system that has no pages loaded in the memory and uses the FIFO Page replacement algorithm. Consider the following reference string: 
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 
Case-1: If the system has 3 frames, the given reference string the using FIFO page replacement algorithm yields a total of 9 page faults. The diagram below illustrates the pattern of the page faults occurring in the example. 
Case-2: If the system has 4 frames, the given reference string using the FIFO page replacement algorithm yields a total of 10 page faults. The diagram below illustrates the pattern of the page faults occurring in the example. 
It can be seen from the above example that on increasing the number of frames while using the FIFO page replacement algorithm, the number of page faults increased from 9 to 10. 
Note – It is not necessary that every string reference pattern cause Belady anomaly in FIFO but there is certain kind of string references that worsen the FIFO performance on increasing the number of frames. 
Why Stack based algorithms do not suffer Anomaly – All the stack based algorithms never suffer Belady Anomaly because these type of algorithms assigns a priority to a page (for replacement) that is independent of the number of page frames. Examples of such policies are Optimal, LRU and LFU. Additionally these algorithms also have a good property for simulation, i.e. the miss (or hit) ratio can be computed for any number of page frames with a single pass through the reference string. 
In LRU algorithm every time a page is referenced it is moved at the top of the stack, so, the top n pages of the stack are the n most recently used pages. Even if the number of frames is incremented to n+1, top of the stack will have n+1 most recently used pages. 
Similar example can be used to calculate the number of page faults in LRU algorithm. Assuming a system that has no pages loaded in the memory and uses the LRU Page replacement algorithm. Consider the following reference string: 
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5  
Case-1: If the system has 3 frames, the given reference string using the LRU page replacement algorithm yields a total of 10 page faults. The diagram below illustrates the pattern of the page faults occurring in the example. 
Case-2: If the system has 4 frames, the given reference string on using LRU page replacement algorithm, then total 8 page faults occur. The diagram shows the pattern of the page faults in the example. 
Conclusion – Various factors substantially affect the number of page faults, such as reference string length and the number of free page frames available. Anomalies also occur due to the small cache size as well as the reckless rate of change of the contents of the cache. Also, the situation of a fixed number of page faults even after increasing the number of frames can also be seen as an anomaly. Often algorithms like Random page replacement algorithm are also susceptible to Belady’s Anomaly, because it may behave like first in first out (FIFO) page replacement algorithm. But Stack based algorithms are generally immune to all such situations as they are guaranteed to give better page hits when the frames are incremented. 
GATE CS Corner questions – Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them. 
GATE-CS-2001 | Question 21GATE-CS-2009 | Question 8ISRO CS 2011 | Question 73GATE-CS-2016 (Set 2) | Question 30ISRO CS 2016 | Question 48GATE CS Mock 2018 | Question 63
GATE-CS-2001 | Question 21
GATE-CS-2009 | Question 8
ISRO CS 2011 | Question 73
GATE-CS-2016 (Set 2) | Question 30
ISRO CS 2016 | Question 48
GATE CS Mock 2018 | Question 63
 
Pushpender007
rkbhola5
GATE CS
Operating Systems
Operating Systems
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.