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 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)
{
int * arr = new int [frame_size];
for ( int i = 0; i < frame_size; i++) {
arr[i] = -1;
}
int cnt = 0;
int start = 0;
int flag;
int elm;
for ( int i = 0; i < len; i++) {
elm = ref[i];
flag = 0;
for ( int j = 0; j < frame_size; j++) {
if (elm == arr[j]) {
flag = 1;
break ;
}
}
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()
{
int ref[] = { 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
int len = sizeof (ref) / sizeof (ref[0]);
int frame_size = 3;
pageFault(frame_size, ref, len);
frame_size = 4;
pageFault(frame_size, ref, len);
}
|
Java
class GFG
{
static void pageFault( int frame_size,
int []ref, int len)
{
int []arr = new int [frame_size];
for ( int i = 0 ; i < frame_size; i++)
{
arr[i] = - 1 ;
}
int cnt = 0 ;
int start = 0 ;
int flag;
int elm;
for ( int i = 0 ; i < len; i++)
{
elm = ref[i];
flag = 0 ;
for ( int j = 0 ; j < frame_size; j++)
{
if (elm == arr[j])
{
flag = 1 ;
break ;
}
}
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);
}
public static void main (String[] args)
{
int ref[] = { 1 , 2 , 3 , 4 , 1 , 2 ,
5 , 1 , 2 , 3 , 4 , 5 };
int len = ref.length;
int frame_size = 3 ;
pageFault(frame_size, ref, len);
frame_size = 4 ;
pageFault(frame_size, ref, len);
}
}
|
Python3
def pageFault(frame_size, ref, len ):
arr = [ 0 ] * frame_size;
for i in range (frame_size):
arr[i] = - 1 ;
cnt = 0 ;
start = 0 ;
for i in range ( len ):
elm = ref[i];
flag = 0 ;
for j in range (frame_size):
if (elm = = arr[j]):
flag = 1 ;
break ;
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);
ref = [ 1 , 2 , 3 , 4 , 1 , 2 ,
5 , 1 , 2 , 3 , 4 , 5 ];
len = len (ref);
frame_size = 3 ;
pageFault(frame_size, ref, len );
frame_size = 4 ;
pageFault(frame_size, ref, len );
|
C#
using System;
class GFG
{
static void pageFault( int frame_size,
int []refer, int len)
{
int []arr = new int [frame_size];
for ( int i = 0; i < frame_size; i++)
{
arr[i] = -1;
}
int cnt = 0;
int start = 0;
int flag;
int elm;
for ( int i = 0; i < len; i++)
{
elm = refer[i];
flag = 0;
for ( int j = 0; j < frame_size; j++)
{
if (elm == arr[j])
{
flag = 1;
break ;
}
}
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);
}
public static void Main()
{
int []refer = { 1, 2, 3, 4, 1, 2,
5, 1, 2, 3, 4, 5 };
int len = refer.Length;
int frame_size = 3;
pageFault(frame_size, refer, len);
frame_size = 4;
pageFault(frame_size, refer, len);
}
}
|
Javascript
<script>
function pageFault(frame_size, refer, len)
{
let arr = new Array(frame_size);
for (let i = 0; i < frame_size; i++)
{
arr[i] = -1;
}
let cnt = 0;
let start = 0;
let flag;
let elm;
for (let i = 0; i < len; i++)
{
elm = refer[i];
flag = 0;
for (let j = 0; j < frame_size; j++)
{
if (elm == arr[j])
{
flag = 1;
break ;
}
}
if (flag == 0)
{
if (start < frame_size)
{
arr[start] = elm;
start++;
}
else if (start == frame_size)
{
arr[0] = elm;
start = 1;
}
cnt++;
}
}
document.write( "When the number of frames are: " +
frame_size + ", " );
document.write( "the number of page " +
"faults is : " + cnt + "</br>" );
}
let refer = [ 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 ];
let len = refer.length;
let frame_size = 3;
pageFault(frame_size, refer, len);
frame_size = 4;
pageFault(frame_size, refer, len);
</script>
|
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
Last Updated :
08 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...