#include<stdio.h>
int refstring[20],pt[10],u[10],nof,nor;
void accept()
{
int i;
printf("\nEnter the reference string:");
for(i=0;i<nor;i++)
{
printf("[%d]=",i);
scanf("%d",&refstring[i]);
}
}
int search(int s)
{
int i;
for(i=0;i<nof;i++)
if(pt[i]==s)
return(i);
return(-1);
}
void second_chance()
{

int i,j,k,faults=0,sp=0;
for(i=0;i<nor;i++)
{
printf("%d",refstring[i]);
k=search(refstring[i]);
if(k==-1)
{
while(1)
{
if(u[sp]==0)
                                       {
       pt[sp]=refstring[i];
u[sp]=1;
sp=(sp+1)%nof;
faults++;
break;
}
else
{
u[sp]=0;
sp=(sp+1)%nof;
}
}
}
for(j=0;j<nof;j++)
{
printf("\n%3d%3d",pt[j],u[j]);
if(j==sp)
printf("*");
}
}
printf("\t");
printf("Total page faults=%d\n",faults);
}
int main()
{
clrscr();
printf("Enter length of reference string:");
scanf("%d",&nor);
printf("Enter the no. of frames:");
scanf("%d",&nof);
accept();
second_chance();
getch();
}

Post a Comment

Previous Post Next Post