LRU
#include <stdio.h>
int findLRU(int time[],int n)
{
int i,minimum=time[0],pos=0;
for(i=0;i<n;i++)
{
if(time[i]<minimum)
{
minimum=time[i];
pos=i;
}
}
return pos;
}
int main()
{
int i,j,n,no,a[100],frame[10],count=0,flag1,flag2,time[10],pos,fault=0;
printf("\n Enter number of pages");
scanf("%d",&n);
printf("\n Enter the page number:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter number of frames");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]=-1;
printf("\n LRU Algorithm as follow:\n");
// printf("\t refString \t frames\n");
for(i=0;i<n;i++)
{
// printf("%d \t\t",a[i]);
flag1=0;
flag2=0;
for(j=0;j<no;j++)
{
if(frame[j]==a[i])
{
count++;
time[j]=count;
flag1=flag2=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<no;j++)
{
if(frame[j]==-1)
{
count++;
fault++;
frame[j]=a[i];
time[j]=count;
flag2=1;
break;
}
}
}
if(flag2==0)
{
pos=findLRU(time,no);
count++;
fault++;
frame[pos]=a[i];
time[pos]=count;
}
printf("\n");
for(j=0;j<no;j++)
printf("\t%d",frame[j]);
}
printf("\n number of faults are %d:",fault);
return 0;
}
0 Comments
If anyone has Doubts or suggestions please let me know