Page Replacement Algorithms - LRU
//Program on Page Replacement Algorithms - LRU
#include<stdio.h>
void main()
{
int frames[10],n,seq[30],c=0,i,j,n1,x=0,m,k=0,flag=0,l,fc[10],r;
printf("\n enter number of pages : ");
scanf("%d",&n1);
printf("\n enter the pages : ");
for(i=0;i<n1;i++)
scanf("%d",&seq[i]);
printf("enter the number of frames:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
frames[i]=-1;
fc[i]=0;
}
for(x=0,i=0;x<n;i++)
{
for(j=0;j<n;j++)
{
flag=0;
if(seq[i]==frames[j])
{
flag=1;
k++;
fc[j]=k;
break;
}
}
if(flag==0)
{
frames[x]=seq[i];
m=i;
c=c+1;
k++;
fc[x]=k;
x++;
printf("\n \n");
for(l=0;l<x;l++)
printf("\t %d",frames[l]);
}
}
for(i=m+1;i<n1;i++)
{
for(j=0;j<n;j++)
{
flag=0;
if(seq[i]==frames[j])
{
flag=1;
k++;
fc[j]=k;
break;
}
}
if(flag==0)
{
r=fc[0];
j=0;
for(l=1;l<n;l++)
{
if(r>fc[l])
{
r=fc[l];
j=l;
}
}
frames[j]=seq[i];
k++;
fc[j]=k;
c=c+1;
printf("\n \n");
for(l=0;l<n;l++)
printf("\t %d",frames[l]);
}
}
printf("\n \n the no of pages faults:%d",c);
}
output:
No comments:
Post a Comment