Belady’s Anomaly in Page Replacement Algorithms

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In the 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 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 the replacement of a page in memory is required at 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.

What is a Page Fault?

A page fault is a type of interrupt or exception that occurs in a computer’s operating system when a program attempts to access a page of memory that is not currently loaded into physical RAM (Random Access Memory). Instead, the page is stored on disk in a storage space called the page file or swap space.

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. 

What is a Belady’s Anomaly?

Bélá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 

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. 

Belady Anomaly - FIFO

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. 

Belady Anomaly - FIFO

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. 

Belady Anomaly - Case 1

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. 

Belady Anomaly - Case 2

How Can Belady’s Anomaly Be Removed?

A stack-based approach can be used to get rid of Belady’s Algorithm. These are some examples of such algorithms:

  • Optimal Page Replacement Algorithm
  •  Least Recently Used Algorithm (LRU)

These algorithms are based on the idea that if a page is inactive for a long time, it is not being utilised frequently. Therefore, it would be best to forget about this page. This allows for improvised memory management and the abolition of Belady’s anomaly.

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. 

Features of Belady’s Anomaly

  • Page fault rate: Page fault rate is the number of page faults that occur during the execution of a process. Belady’s Anomaly occurs when the page fault rate increases as the number of page frames allocated to a process increases.
  • Page replacement algorithm: Belady’s Anomaly is specific to some page replacement algorithms, including the First-In-First-Out (FIFO) algorithm and the Second-Chance algorithm.
  • System workload: Belady’s Anomaly can occur when the system workload changes. Specifically, it can happen when the number of page references in the workload increases.
  • Page frame allocation: Belady’s Anomaly can occur when the page frames allocated to a process are increased, but the total number of page frames in the system remains constant. This is because increasing the number of page frames allocated to a process reduces the number of page faults initially, but when the workload increases, the increased number of page frames can cause the process to evict pages from its working set more frequently, leading to more page faults.
  • Impact on performance: Belady’s Anomaly can significantly impact system performance, as it can result in a higher number of page faults and slower overall system performance. It can also make it challenging to choose an optimal number of page frames for a process.

Advantages

  • Better insight into algorithm behavior: Belady’s Anomaly can provide insight into how a page replacement algorithm works and how it can behave in different scenarios. This can be helpful in designing and optimizing algorithms for specific use cases.
  • Improved algorithm performance: In some cases, increasing the number of frames allocated to a process can actually improve algorithm performance, even if it results in more page faults. This is because having more frames can reduce the frequency of page replacement, which can improve overall performance.

Disadvantages

  • Poor predictability: Belady’s Anomaly can make it difficult to predict how an algorithm will perform with different configurations of frames and pages, which can lead to unpredictable performance and system instability.
  • Increased overhead: In some cases, increasing the number of frames allocated to a process can result in increased overhead and resource usage, which can negatively impact system performance.
  • Unintuitive behavior: Belady’s Anomaly can result in unintuitive behavior, where increasing the number of frames allocated to a process results in more page faults, which can be confusing for users and system administrators.
  • Difficulty in optimization: Belady’s Anomaly can make it difficult to optimize page replacement algorithms for specific use cases, as the behavior of the algorithm can be unpredictable and inconsistent.

Conclusion

A page fault occurs when a program accesses a page not currently in RAM, triggering a process to load the page from disk. Belady’s Anomaly describes the counterintuitive situation where increasing the number of memory frames can lead to more page faults with some algorithms, like FIFO. This anomaly highlights the importance of choosing efficient page replacement algorithms. Stack-based algorithms, such as LRU and Optimal, are immune to Belady’s Anomaly and typically provide more predictable performance.

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. 

  1. GATE-CS-2001 | Question 21
  2. GATE-CS-2009 | Question 8
  3. ISRO CS 2011 | Question 73
  4. GATE-CS-2016 (Set 2) | Question 30
  5. ISRO CS 2016 | Question 48
  6. GATE CS Mock 2018 | Question 63

Frequently Asked Questions on Belady’s Anomaly – FAQs

What is a page fault?

A page fault occurs when a program tries to access a page of memory that is not currently loaded into physical RAM. The operating system must then load the required page from disk into RAM, which can cause a delay in execution.

What is Belady’s Anomaly?

Belady’s Anomaly is a phenomenon where increasing the number of page frames allocated to a process results in an increase in the number of page faults, contrary to the expected decrease. This anomaly is typically observed in certain page replacement algorithms, such as FIFO.

Which page replacement algorithms are affected by Belady’s Anomaly?

Belady’s Anomaly commonly affects algorithms like First-In-First-Out (FIFO), Second-Chance, and Random page replacement. These algorithms do not follow stack-based policies and can show increased page faults with more frames.

How can Belady’s Anomaly be avoided?

Belady’s Anomaly can be avoided by using stack-based page replacement algorithms, such as Least Recently Used (LRU) or Optimal Page Replacement. These algorithms ensure that increasing the number of frames does not lead to more page faults.



Previous Article
Next Article

Similar Reads

Program to show Belady's Anomaly
Prerequisite - Belady's Anomaly Belady's Anomaly is when the number of page faults increases even after increasing the number of frames. In this article, we demonstrate Belady's Anomaly using FIFO page replacement algorithm.Examples: Reference array is: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Output : When number of frames is 3, page fault : 9 When numb
8 min read
Advantages and Disadvantages of various Page Replacement algorithms
Page Scheduling, involves many different algorithms which have their Advantages and Disadvantages. 1. First In First Out (FIFO): Advantages -It is simple and easy to understand & implement.It is efficiently used for small systemsIt does not cause more overheadsSimplicity: FIFO is a simple and easy-to-implement algorithm. It does not require com
6 min read
Difference Between LRU and FIFO Page Replacement Algorithms in Operating System
Page replacement algorithms are used in operating systems to manage memory effectively. Page replacement algorithms are essential in operating systems for efficient memory management. These algorithms select which memory pages should be changed when a new page is brought. Least Recently Used (LRU) and First-In-First-Out (FIFO) are two popular page
3 min read
Page Replacement Algorithms in Operating Systems
In an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page replacement becomes necessary when a page fault occurs and no free page frames are in memory. However, another page fault would arise if the replaced page is referenced again.
8 min read
Optimal Page Replacement Algorithm
Prerequisite: Page Replacement Algorithms In operating systems, 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
15+ min read
Second Chance (or Clock) Page Replacement Policy
Prerequisite - Page Replacement Algorithms Apart from LRU, OPT and FIFO page replacement policies, we also have the second chance/clock page replacement policy. In the Second Chance page replacement policy, the candidate pages for removal are considered in a round robin matter, and a page that has been accessed between consecutive considerations wi
15+ min read
Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
Prerequisite - Least Recently Used (LRU) Page Replacement algorithm Least Recently Used page replacement algorithm replaces the page which is not used recently. Implementation: In this article, LRU is implemented using counters, a ctime (i.e., counter) variable is used to represent the current time, it is incremented for every page of the reference
8 min read
Not Recently Used (NRU) page replacement algorithm
It is a page replacement algorithm. This algorithm removes a page at random from the lowest numbered non-empty class. Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in atleast one clock tick than a clean page that is in heavy use. It is easy to understand, moderately efficient to implement and
3 min read
Counting Based Page Replacement Algorithm in Operating System
Counting Based Page Replacement Algorithm replaces the page based on count i.e. number of times the page is accessed in the past. If more than one page has the same count, then the page that occupied the frame first would be replaced. Page Replacement: Page Replacement is a technique of replacing a data block (frame) of Main Memory with the data bl
5 min read
Program for Least Recently Used (LRU) Page Replacement algorithm
Prerequisite: Page Replacement AlgorithmsIn 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
14 min read
Page Table Entries in Page Table
A Page Table is a data structure used by the operating system to keep track of the mapping between virtual addresses used by a process and the corresponding physical addresses in the system's memory. A Page Table Entry (PTE) is an entry in the Page Table that stores information about a particular page of memory. Each PTE contains information such a
7 min read
Difference Between Page Table and Inverted Page Table
Paging is a concept for storage management in operating systems that retrieve processes from secondary storage and store them in main memory as pages. The main concept behind paging is to divide each process into pages. Frames will also be used to divide the main memory. One page of the process is to be saved in one of the memory frames. In various
5 min read
Caching Page Tables
Paging is a memory management scheme which allows physical address space of a process to be non-contiguous. The basic idea of paging is to break physical memory into fixed-size blocks called frames and logical memory into blocks of same size called pages. While executing the process the required pages of that process are loaded into available frame
6 min read
Page Faults in LFU | Implementation
In this, it is using the concept of paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when the new page comes in. Whenever a new page is referred to and is not present in memory, the page fault occurs and the Operating System replaces one of the existing pages with a newly needed page. LF
12 min read
Difference between page and block in operating system
In this article, we will discuss the overview of the page and block in Operating System and then will discuss the differences by mentioning features of both. Let's discuss it one by one. Block Overview :Block is the smallest unit of data storage. It is used to read a file or write data to a file. Block is also a sequence of bits and bytes. Block is
5 min read
Page Buffering Algorithm in Operating System
The Page Buffering Algorithm is used in Operating systems and Database Management Systems as a key method to streamline data access and minimize disc I/O operations. It is largely used in virtual memory systems, where data is kept on secondary storage (disc) and brought into main memory as needed. The Page Buffering Algorithm's primary goal is to r
7 min read
Hashed Page Tables in Operating System
There are several common techniques for structuring page tables like Hierarchical Paging, Hashed Page Tables, and Inverted Page Tables. In this article, we will discuss the Hashed Page Table. Hashed Page Tables are a type of data structure used by operating systems to efficiently manage memory mappings between virtual and physical memory addresses.
4 min read
Page Faults in LRU | Implementation
LRU uses the concept of paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when the new page comes in. Whenever a new page is referred to and is not present in memory, the page fault occurs and the Operating System replaces one of the existing pages with a newly needed page. LRU is one suc
15+ min read
Page Fault Handling in Operating System
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. This triggers a sequence of events where the operating system must manage the fault by loading the required data from secondary storage into RAM. Page faults are essential for implementing virtual memory sy
5 min read
Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
Prerequisite - CPU Scheduling Priority Scheduling : In priority scheduling, each process has a priority which is an integer value assigned to it. The smallest integer is considered as the highest priority and the largest integer is considered as the lowest priority. The process with the highest priority gets the CPU first. In rare systems, the larg
4 min read
FCFS Disk Scheduling Algorithms
Prerequisite: Disk scheduling algorithms.Given an array of disk track numbers and initial head position, our task is to find the total number of seek operations done to access all the requested tracks if First Come First Serve (FCFS) disk scheduling algorithm is used. First Come First Serve (FCFS) FCFS is the simplest disk scheduling algorithm. As
6 min read
Advantages and Disadvantages of various CPU scheduling algorithms
CPU Scheduling involves many different scheduling algorithms which have their Advantages and Disadvantages. 1. First Come First Serve (FCFS): Advantages: It is simple and easy to understand.FCFS provides fairness by treating all processes equally and giving them an equal opportunity to run.FCFS guarantees that every process will eventually get a ch
3 min read
Difference between SCAN and LOOK Disk scheduling algorithms
SCAN disk scheduling algorithm: In SCAN disk scheduling algorithm, head starts from one end of the disk and moves towards the other end, servicing requests in between one by one and reach the other end. Then the direction of the head is reversed and the process continues as head continuously scan back and forth to access the disk. So, this algorith
4 min read
Difference between SJF and LJF CPU scheduling algorithms
Shortest Job First: The shortest job first (SJF) algorithm is a CPU scheduling algorithm designed to reorder the jobs so that the process having the smallest burst time is chosen for the next execution. It is used to reduce the average waiting time for other processes waiting for execution. This may be preemptive or non-preemptive. Its preemptive v
4 min read
Difference between FCFS and SCAN disk scheduling algorithms
FCFS disk scheduling algorithm: As the name suggests, the FCFS Scheduling Algorithm processes requests in the sequential order in which they arrive in the disk queue. Even if a higher priority request arrives in the schedule later, FCFS will process the request in the order that they have arrived and hence we can say that FCFS has a fair policy. Ex
3 min read
Difference between SCAN and CSCAN Disk scheduling algorithms
Prerequisite - Disk Scheduling Algorithms 1. SCAN Disk Scheduling Algorithm : SCAN Disk Scheduling Algorithm is also known as Elevator algorithm. In this, head or pointer can move in both the direction i.e., disk arm starts to move from one end of the disk to the other end servicing all the requests until it reaches to the other end of the disk. Af
3 min read
Advantages and Disadvantages of various Disk scheduling algorithms
Prerequisite - Disk Scheduling Algorithms 1. First Come First Serve (FCFS) : Advantages - First Come First Serve algorithm has a very simple logic, it executes the process requests one by one in the sequence they arrive. Thus, First Come First Serve is very simple and easy to understand and implement. In FCFS eventually, each and every process gets
3 min read
Difference between FCFS and SJF CPU scheduling algorithms
1. First Come First Served (FCFS) : First Come First Served (FCFS) is the simplest type of algorithm. It is a non-preemptive algorithm i.e. the process cannot be interrupted once it starts executing. The FCFS is implemented with the help of a FIFO queue. The processes are put into the ready queue in the order of their arrival time. The process that
3 min read
Difference between SJF and SRJF CPU scheduling algorithms
1. Shortest Job First (SJF) : The Shortest Job First (SJF) is a scheduling policy that selects the waiting process with the smallest execution time to execute next. It is also known as Shortest Job Next (SJN) or Shortest Process Next (SPN). It is a non-preemptive scheduling algorithm. 2. Shortest Remaining Job First (SRTF) : The Shortest Remaining
2 min read
Difference between SRJF and LRJF CPU scheduling algorithms
1. Shortest remaining job first (SRJF) : Shortest remaining job first also called the shortest remaining time first is the preemptive version of the shortest job first scheduling algorithm. In the shortest remaining job first, the process with the smallest runtime to complete (i.e remaining time) is scheduled to run next, In SRJF, a running process
3 min read
Article Tags :