Page Replacement Algorithms - Optimal Page Replacement
//program on page replacement alogarthim -optimal
#include<stdio.h>
main()
{
int frames[10],n,seq[30],c=0,i,j,nl,x=0,m,k,flag=0,l,mf[10];
int c1,c2,c3,m1,m2;
printf("enter number of pages:");
scanf("%d",&nl);
printf("\n enter pages:");
for(i=0;i<nl;i++)
scanf("%d",&seq[i]);
printf("\n enter no of frames:");
scanf("%d",&n);
for(i=0;i<n;i++)
frames[i]=-1;
for(x=0,i=0;x<n;i++)
{
for(j=0;j<n;j++)
{
flag=0;
if(seq[i]==frames[j])
{
flag=1;
break;
}
}
if(flag==0)
{
frames[x]=seq[i];
m=i;
x++;
c=c+1;
printf("\n \n");
for(k=0;k<x;k++)
printf("\t %d",frames[k]);
}
}
for(i=m+1;i<nl;i++)
{
for(j=0;j<n;j++)
{
flag=0;
if(seq[i]==frames[j])
{
flag=1;
break;
}
}
if(flag==0)
{
for(k=0;k<n;k++)
mf[k]=0;
c1=0;c2=0;c3=0;
for(j=i+1;j<nl;j++)
{
for(k=0;k<n;k++)
{
if(frames[k]==seq[j])
{
mf[k]=mf[k]+1;
if(k==0)
c1=1;
else if(k==1)
c2=1;
else if(k==2)
c3=1;
break;
}
}
if(((c1==1)&&(c2==1))||((c2==1)&&(c3==1))||((c3==1)&&(c1==1)))
break;
}
m1=0;
m2=mf[0];
for(k=0;k<n;k++)
{
if(mf[k]<m2)
{
m1=k;
m2=mf[k];
}
}
frames[m1]=seq[i];
c=c+1;
printf("\n \n");
for(l=0;l<n;l++)
printf("\t %d",frames[l]);
}
}
printf("\n\n\n the n of pages faults=%d",c);
}
#include<stdio.h>
main()
{
int frames[10],n,seq[30],c=0,i,j,nl,x=0,m,k,flag=0,l,mf[10];
int c1,c2,c3,m1,m2;
printf("enter number of pages:");
scanf("%d",&nl);
printf("\n enter pages:");
for(i=0;i<nl;i++)
scanf("%d",&seq[i]);
printf("\n enter no of frames:");
scanf("%d",&n);
for(i=0;i<n;i++)
frames[i]=-1;
for(x=0,i=0;x<n;i++)
{
for(j=0;j<n;j++)
{
flag=0;
if(seq[i]==frames[j])
{
flag=1;
break;
}
}
if(flag==0)
{
frames[x]=seq[i];
m=i;
x++;
c=c+1;
printf("\n \n");
for(k=0;k<x;k++)
printf("\t %d",frames[k]);
}
}
for(i=m+1;i<nl;i++)
{
for(j=0;j<n;j++)
{
flag=0;
if(seq[i]==frames[j])
{
flag=1;
break;
}
}
if(flag==0)
{
for(k=0;k<n;k++)
mf[k]=0;
c1=0;c2=0;c3=0;
for(j=i+1;j<nl;j++)
{
for(k=0;k<n;k++)
{
if(frames[k]==seq[j])
{
mf[k]=mf[k]+1;
if(k==0)
c1=1;
else if(k==1)
c2=1;
else if(k==2)
c3=1;
break;
}
}
if(((c1==1)&&(c2==1))||((c2==1)&&(c3==1))||((c3==1)&&(c1==1)))
break;
}
m1=0;
m2=mf[0];
for(k=0;k<n;k++)
{
if(mf[k]<m2)
{
m1=k;
m2=mf[k];
}
}
frames[m1]=seq[i];
c=c+1;
printf("\n \n");
for(l=0;l<n;l++)
printf("\t %d",frames[l]);
}
}
printf("\n\n\n the n of pages faults=%d",c);
}
output:
No comments:
Post a Comment