Skip to content

Instantly share code, notes, and snippets.

@sumedhahire
Forked from projjal1/fifo.c
Last active December 15, 2022 16:07
Show Gist options
  • Save sumedhahire/1e22ffd9b2df3e55216e851d0ebc3a86 to your computer and use it in GitHub Desktop.
Save sumedhahire/1e22ffd9b2df3e55216e851d0ebc3a86 to your computer and use it in GitHub Desktop.
FIFO Page Replacement Algorithm Implementation in C
#include<stdio.h>
void fifo(int string[20],int n,int size)//n no frames
{
//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=0;
int i,j;
//Counters
int page_miss=0;
int page_hits=0;
//Traversing each symbol in fifo
for (i=0;i<size;i++)
{
//int symbol=string[i];//7,
int flag=0;
for(j=0;j<n;j++)
{
if (string[i]==frames[j])
{
flag=1;
break;
}
}
if (flag==1)
{
page_hits+=1;
}
else
{
//replacment for =>index=(index+1)%n (only for understanding)
if(index<2){
frames[index]=string[i];
page_miss+=1;
index++;
}else if(index==2){
frames[index]=string[i];
page_miss+=1;
index=0;
}
}
}
printf("\nPage hits: %d",page_hits);
printf("\nPage misses: %d",page_miss);
}
//Main function
int main(void)
{
int string[]={7,0,1,2,0,3,0,4,2};
int no_frames=3;
//gets the size of string[]
//if we have 4 int values that means 4*4=16 to sum it up from 4 bytes to one
//byte(1 int is 4 but we need only 1 for count) we divide it with one int
//4*4=16 then 16/4=4(we got size)
//1*4=4 then 4/4=1
int size=sizeof(string) / sizeof(int);//int size is 4
printf("%d",size);
fifo(string,no_frames,size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment