#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define max 100
#define min 10
int ref[max],count,frame[min],n;
void input()
{
system("CLS");
int i,temp;
count=0;
printf("\n\n\tEnter the number of page frames : ");
scanf("%d",&n);
printf("\n\n\tEnter the reference string (-1 for end) : ");
scanf("%d",&temp);
while(temp != -1)
{
ref[count++]=temp;
scanf("%d",&temp);
}
}
void FIFO()
{
int i,j,fault=0;
system("CLS");
for(i=0;i<n;i++)
frame[i]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
if(j==n)
frame[fault%n]=ref[i], fault++;
printf("\n\nAfter inserting %d the frame status is : ",ref[i]);
for(j=0;j<n;j++)
printf("%d ",frame[j]);
getch();
}
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}
void Second_Chance()
{
int i,j,ref_bit[min],cur=0,fault=0;
system("CLS");
for(i=0;i<n;i++)
ref_bit[i]=0, frame[i]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
if(j==n)
while(1)
{
if(cur==n)
cur=0;
if(ref_bit[cur]==0)
{
frame[cur]=ref[i];
ref_bit[cur]=1;
fault++;
cur++;
break;
}
else
ref_bit[cur]=0;
cur++;
}//while
printf("\n\nAfter inserting %d the frame status is : ",ref[i]);
printf("\nFrame Reference_bit\n");
for(j=0;j<n;j++)
printf(" %d %d\n",frame[j],ref_bit[j]);
getch();
}//for i
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}
void LRU()
{
int i,j,k,stack[min],top=0,fault=0;
system("CLS");
for(i=0;i<count;i++)
{
if(top<n)
stack[top++]=ref[i],fault++;
else
{
for(j=0;j<n;j++)
if(stack[j]==ref[i])
break;
if(j<n)
{
for(k=j;k<n-1;k++)
stack[k]=stack[k+1];
stack[k]=ref[i];
}
else
{
for(k=0;k<n-1;k++)
stack[k]=stack[k+1];
stack[k]=ref[i];
fault++;
}
}
printf("\n\nAfter inserting %d the stack status is : ",ref[i]);
for(j=0;j<top;j++)
printf("%d ",stack[j]);
getch();
}//for i
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}
void NRU()
{
int i,j,Class[min],min_class,rb,mb,cl,index,fault=0;
system("CLS");
for(j=0;j<n;j++)
Class[j]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
printf("\nEnter the referenced & modified bit status for %d : ",ref[i]);
scanf("%d%d",&rb,&mb);
if(rb==0 && mb==0)
cl=0;
else if(rb==0 && mb==1)
cl=1;
else if(rb==1 && mb==0)
cl=2;
else
cl=3;
if(j<n)
Class[j]=cl;
else
{
min_class=Class[0];
index=0;
for(j=1;j<n;j++)
if(min_class>Class[j])
min_class=Class[j], index=j;
frame[index]=ref[i];
Class[index]=cl;
fault++;
}//else
printf("\n\nAfter inserting %d the frame status is : ",ref[i]);
printf("Frame Class");
for(j=0;j<n;j++)
printf("%d %d\n",frame[j],Class[j]);
getch();
}//for i
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}
void main()
{
int x;
//freopen("in.cpp","r",stdin);
while(1)
{
system("CLS");
printf("\n\n\t-----MENU-----");
printf("\n\t1. Input ");
printf("\n\t2. FIFO Algorithm");
printf("\n\t3. Second Chance Algorithm");
printf("\n\t4. LRU (Least Recently Used) Algorithm");
printf("\n\t5. NRU (Not Recently Used) Algorithm");
printf("\n\t0. Exit.");
printf("\n\n\tEnter your choice.");
scanf("%d",&x);
switch(x)
{
case 1:
input();
break;
case 2:
FIFO();
break;
case 3:
Second_Chance();
break;
case 4:
LRU();
break;
case 5:
NRU();
break;
case 0:
exit(0);
}
}
}