#include<stdio.h>
int main()
{
int i,j,k,l,Place,f0=0,f1=0,f2=0,f=0,pagefault=0;
int temp[3];
int Frame[3]; //Defining a three frame architecture
int Page[20]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1}; //Set of pages
printf("\n\nList of pages:>\n");
for(i=0;i<20;i++)
{
printf("%d ",Page[i]);//Printing pages
}
printf("\n\nList of Content of frame\n");
Frame[0]=Page[0]; // Initialising
Frame[1]=Page[1]; // first three pages to the
Frame[2]=Page[2]; // Frame
printf("%d %d %d \n",Frame[0],Frame[1],Frame[2]);//Priniting the content of the frame
for(i=3;i<20;i++)
{
for(j=0;j<3;j++)
{
f=0;
if(Frame[j]==Page[i]) // Checking whether the current page is in the Frame
{
f=1;break;
}
}
if(f!=1) //If not
{
for(k=i-1;k>=0;k--)
{
if(f0==0 && Page[k]==Frame[0]){f0=1;} //Checking the
if(f1==0 && Page[k]==Frame[1]){f1=1;}// page to be
if(f2==0 && Page[k]==Frame[2]){f2=1;}//replaced
if(f0==1 && f1==1){Frame[2]=Page[i];pagefault++;break;}else//Replacing the least
if(f0==1 && f2==1){Frame[1]=Page[i];pagefault++;break;}else// recently used page and
if(f1==1 && f2==1){Frame[0]=Page[i];pagefault++;break;}// caculating the page fault
}
printf("%d %d %d \n",Frame[0],Frame[1],Frame[2]);//Printing the content of the frame
}
f0=0;f1=0;f2=0;f=0;//Resetting all the flag variables
}
printf("\n\n\tNumber of page faults <%d>\n",pagefault); //Printing the number of page faults
}