I am trying to do page replacement algorithm in C. I wrote the code below. I have written similar code that had worked below. But now when I try to reproduce the code enters an infinite loop.
I checked the for loop for hit and miss and it becomes stationary while current increments are unlimited. I have been tweaking the program for some time but nothing seems to work.
#include<stdio.h>
#include<stdlib.h>
int n,table[20],fsize,i,j,request[20],arr2[20],current,miss;
void initialize(int arr[],int n){ //set frame entries to -1
for(i=0;i<n;i++){
arr[i]=-1;
}
}
int isavailable(int arg){ //checks if entry is available in frame table (array)
int present=0;
for(i=0;i<fsize;i++){
if(table[i]==arg){
present=1;
break;
}
}
return present;
}
void fifo(){ //first in first out
miss=0;
current=-1;
initialize(table,fsize);
for(i=0;i<n;i++){
arr2[i]=request[i];
}
for(i=0;i<n;i++){
if(isavailable(arr2[i])==1){ //hit condition
printf("Hit");
continue;
}
current=(current+1)%fsize; //interates in a cycle during miss
table[current]=arr2[i]; //places in first entered location
miss++;
}
printf("Total misses: %d",miss);
}
void main(){
printf("Enter number of requests: ");
scanf("%d",&n);
printf("Enter table size: "); //reads frame table size
scanf("%d",&fsize);
printf("Enter requests: ");
for(i=0;i<n;i++){
scanf("%d",&request[i]); //reads request sequence in array
}
printf("FIFO: ");
fifo();
printf("LRU: ");
//lru();
printf("Optimal: ");
//optimal();
}
Edit:- The input i gave are:-
- no of requests: 7
- sizeoftable: 3
- request: 1,3,0,3,5,6,3