/*A PROGRAM TO SIMULATE FIFO PAGE REPLACEMENT ALGORITHM */
#include<stdio.h>
#include<conio.h>
void main()
{
int rs[30],pf[10],ctr[10]={0};
int n,i,j,k,m,c=0,g,p,pos,count,count1=0,nr;
clrscr();
printf("\nEnter Howmany Frames The Page Have::");
scanf("%d",&n);
printf("\nEnter Howmany Numbers The Reference Strng Have::");
scanf("%d",&nr);
printf("\nEnter Reference String::");
for(i=0;i<nr;i++)
{
scanf("%d",&rs[i]);
for(k=0;k<10;k++)
if(rs[i]==k)
{
ctr[k]++;
break;
}
}
printf("\nSIMULATION OF PAGE REPLACEMENT ALGORITHM::FIFO\n");
printf("\n\nThe Pages Containing Page Faults One By One::\n\n");
//pf[0]=0;
for(i=0;i<n;i++)
{
for(m=0;m<=i&&i!=0;m++)
if(pf[m]==rs[i])
{
i++;
count=1;
break;
}
if(count==1)
continue;
pf[i]=rs[i];
for(k=0;k<=i;k++)
printf("\t%d\n",pf[k]);
c++;
printf("\t-------\n");
}
do
{
count1=0;
for(m=0;m<n;m++)
if(pf[m]==rs[i])
{
count1=1;
break;
}
if(count1==0)
{
g=n;
for(p=0;p<n-1;p++)
{
if(ctr[pf[p]]==ctr[pf[p+1]]&&g>ctr[pf[p]])
for(m=0;m<n;m++)
if(pf[m]==rs[i-n])
pos=m;
if(ctr[pf[p]]<ctr[pf[p+1]])
{ pos=p;
g=ctr[pf[p]];
}
else if(ctr[pf[p]]>ctr[pf[p+1]])
{
pos=p+1;
g=ctr[pf[p+1]];
}
}
pf[pos]=rs[i];
for(k=0;k<n;k++)
printf("\t%d\n",pf[k]);
printf("\t--------\n");
c++;
}
i++;
}while(i!=nr)
printf("\nNumber Of Page Faults::%d",c);
getch();
}
No comments:
Post a Comment