Skip to content

Instantly share code, notes, and snippets.

@projjal1
Created May 13, 2020 13:43
Show Gist options
  • Save projjal1/9f406be8764d0709f98561364a53547a to your computer and use it in GitHub Desktop.
Save projjal1/9f406be8764d0709f98561364a53547a to your computer and use it in GitHub Desktop.
Fifo Page Replacement Algorithm to show Belady's Anomaly
#include<stdio.h>
void fifo(int string[20],int n,int size)
{
//Creating array for block storage
int frames[n];
//Initializing each block with -1
for (int i=0;i<n;i++)
frames[i]=-1;
//Index to insert element
int index=-1;
//Counters
int page_miss=0;
int page_hits=0;
//Traversing each symbol in fifo
for (int i=0;i<size;i++)
{
int symbol=string[i];
int flag=0;
for(int j=0;j<n;j++)
{
if (symbol==frames[j])
{
flag=1;
break;
}
}
if (flag==1)
{
page_hits+=1;
}
else
{
index=(index+1)%n;
frames[index]=symbol;
page_miss+=1;
}
}
printf("\nPage hits: %d",page_hits);
printf("\nPage misses: %d",page_miss);
}
//Main function
int main(void)
{
int string1[]={1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
int size=sizeof(string1)/sizeof(int);
//With 2 frames
printf("\nWith 2 frames");
fifo(string1,2,size);
int string2[]={1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
//With 3 frames
printf("\n\nWith 3 frames");
fifo(string2,3,size);
int string3[]={1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
//With 4 frames
printf("\n\nWith 4 frames");
fifo(string3,4,size);
int string4[]={1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
//With 5 frames
printf("\n\nWith 5 frames");
fifo(string4,5,size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment