Skip to content
Related Articles

Related Articles

Program to show Belady’s Anomaly
  • Last Updated : 27 Nov, 2019

Prerequisite – Belady’s Anomaly
Belady’s Anomaly is when the number of page faults increase 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 number of frames is 4, page fault : 10

Reference array is: 0, 1, 5, 3, 0, 1, 4, 0, 1, 5, 3, 4
Output :
When number of frames is 3, page fault : 9
When number of frames is 4, page fault : 10 

Implementation :
FIFO page replacement algorithm is used to showcase the Belady’s Anomaly. Firstly, an array of size equal to the number of frames is used, this array simulates the page frames, the operations on this array are performed in a circular array type fashion. A count variable is used to calculate the page fault, this variable is incremented whenever an element is inserted / rewritten in the page frame array.

In this code:
Two frame sizes 3 and 4 are tested respectively, with increase in number of frames the page faults should have decreased, but an anomaly is witnessed as 3 frames result in 9 page faults whereas 4 frames result in 10 page faults. This is Belady’s Anomaly, it is observed in special cases of reference array and frame size.

C++




#include <bits/stdc++.h>
  
using namespace std;
  
void pageFault(int frame_size, int* ref, int len)
{
    // To dynamically allocate an array,
    // it represents the page frames.
    int* arr = new int[frame_size];
  
    // To initialize the array
    for (int i = 0; i < frame_size; i++) {
        arr[i] = -1;
    }
  
    // To count page faults
    int cnt = 0;
    int start = 0;
    int flag;
    int elm;
  
    for (int i = 0; i < len; i++) {
        elm = ref[i];
        // Linear search to find if the page exists
        flag = 0;
        for (int j = 0; j < frame_size; j++) {
            if (elm == arr[j]) {
                flag = 1;
                break;
            }
        }
        // If the page doesn't exist it is inserted,
        // count is incremented
        if (flag == 0) {
            if (start < frame_size) {
                arr[start] = elm;
                start++;
            }
            else if (start == frame_size) {
                arr[0] = elm;
                start = 1;
            }
            cnt++;
        }
    }
    cout << "When the number of frames are: " << frame_size << ", ";
    cout << "the number of page faults is : " << cnt << endl;
}
  
int main()
{
    // Reference array
    int ref[] = { 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
    int len = sizeof(ref) / sizeof(ref[0]);
    // The frame size
    int frame_size = 3;
  
    pageFault(frame_size, ref, len);
  
    // Increase value of frame size
    frame_size = 4;
  
    // The page fault increases
    // even after increasing the
    // the number of frames.
    // This is Belady's Anomaly
    pageFault(frame_size, ref, len);
}

Java




// Java Implementation of the above approach
class GFG 
{
static void pageFault(int frame_size, 
                      int []ref, int len) 
    // To dynamically allocate an array, 
    // it represents the page frames. 
    int []arr = new int[frame_size]; 
  
    // To initialize the array 
    for (int i = 0; i < frame_size; i++)
    
        arr[i] = -1
    
  
    // To count page faults 
    int cnt = 0
    int start = 0
    int flag; 
    int elm; 
  
    for (int i = 0; i < len; i++) 
    
        elm = ref[i]; 
          
        // Linear search to find 
        // if the page exists 
        flag = 0
        for (int j = 0; j < frame_size; j++) 
        
            if (elm == arr[j]) 
            
                flag = 1
                break
            
        
          
        // If the page doesn't exist it is inserted, 
        // count is incremented 
        if (flag == 0)
        
            if (start < frame_size) 
            
                arr[start] = elm; 
                start++; 
            
            else if (start == frame_size)
            
                arr[0] = elm; 
                start = 1
            
            cnt++; 
        
    
    System.out.print("When the number of frames are: " +
                                     frame_size + ", "); 
    System.out.println("the number of page faults is : " + cnt); 
  
// Driver Code
public static void main (String[] args) 
    // Reference array 
    int ref[] = { 1, 2, 3, 4, 1, 2
                  5, 1, 2, 3, 4, 5 }; 
      
    int len = ref.length; 
      
    // The frame size 
    int frame_size = 3
  
    pageFault(frame_size, ref, len); 
  
    // Increase value of frame size 
    frame_size = 4
  
    // The page fault increases 
    // even after increasing the 
    // the number of frames. 
    // This is Belady's Anomaly 
    pageFault(frame_size, ref, len); 
}
  
// This code is contributed by AnkitRai01

Python3




# Python3 Implementation of the above approach
def pageFault(frame_size, ref, len):
      
    # To dynamically allocate an array,
    # it represents the page frames.
    arr = [0] * frame_size;
  
    # To initialize the array
    for i in range(frame_size):
        arr[i] = -1;
  
    # To count page faults
    cnt = 0;
    start = 0;
  
    for i in range(len):
        elm = ref[i];
          
        # Linear search to find if the page exists
        flag = 0;
        for j in range(frame_size):
            if (elm == arr[j]):
                flag = 1;
                break;
                  
        # If the page doesn't exist it is inserted,
        # count is incremented
        if (flag == 0):
            if (start < frame_size):
                arr[start] = elm;
                start += 1;
            elif (start == frame_size):
                arr[0] = elm;
                start = 1;
            cnt += 1;
  
    print("When the number of frames are: ",
                    frame_size, end = ", ");
    print("the number of page faults is : ", cnt);
  
# Driver Code
  
# Reference array
ref = [1, 2, 3, 4, 1, 2,
       5, 1, 2, 3, 4, 5 ];
len = len(ref);
  
# The frame size
frame_size = 3;
  
pageFault(frame_size, ref, len);
  
# Increase value of frame size
frame_size = 4;
  
# The page fault increases
# even after increasing the
# the number of frames.
# This is Belady's Anomaly
pageFault(frame_size, ref, len);
  
# This code is contributed by 29AjayKumar

C#




// C# Implementation of the above approach
using System;
  
class GFG 
{
static void pageFault(int frame_size, 
                      int []refer, int len) 
    // To dynamically allocate an array, 
    // it represents the page frames. 
    int []arr = new int[frame_size]; 
  
    // To initialize the array 
    for (int i = 0; i < frame_size; i++)
    
        arr[i] = -1; 
    
  
    // To count page faults 
    int cnt = 0; 
    int start = 0; 
    int flag; 
    int elm; 
  
    for (int i = 0; i < len; i++) 
    
        elm = refer[i]; 
          
        // Linear search to find 
        // if the page exists 
        flag = 0; 
        for (int j = 0; j < frame_size; j++) 
        
            if (elm == arr[j]) 
            
                flag = 1; 
                break
            
        
          
        // If the page doesn't exist it is inserted, 
        // count is incremented 
        if (flag == 0)
        
            if (start < frame_size) 
            
                arr[start] = elm; 
                start++; 
            
            else if (start == frame_size)
            
                arr[0] = elm; 
                start = 1; 
            
            cnt++; 
        
    
    Console.Write("When the number of frames are: "
                                  frame_size + ", "); 
    Console.WriteLine("the number of page "
                       "faults is : " + cnt); 
  
// Driver Code
public static void Main() 
    // Reference array 
    int []refer = { 1, 2, 3, 4, 1, 2, 
                    5, 1, 2, 3, 4, 5 }; 
      
    int len = refer.Length; 
      
    // The frame size 
    int frame_size = 3; 
  
    pageFault(frame_size, refer, len); 
  
    // Increase value of frame size 
    frame_size = 4; 
  
    // The page fault increases 
    // even after increasing the 
    // the number of frames. 
    // This is Belady's Anomaly 
    pageFault(frame_size, refer, len); 
}
  
// This code is contributed by kanugargng
Output:
When the number of frames are: 3, the number of page faults is : 9
When the number of frames are: 4, the number of page faults is : 10

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :