Thursday, 28 April 2011

Page Replacement Algorithms..........


/****************************************
* TE-A-37 Date:-  /  /2011
* Assignment No:-
Page Replacement Algorithms:-
(1) FIFO
(2) Least Recent Unit (LRU)
(3) Optimal Page Replacement (OPT)
****************************************/
#include<stdio.h>
#include<process.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<op.h>
/****************************************/
int i,ii,j,k,q[3],l,m,gd,gm,value,mybrk,row,col;
int prio[4],temp[4],level,arr[96],optsize;
int optindex,check[3],grow[96],pos,jj,posval;
int flag1,flag2,flag3,ch,pgfault;
char *aaa,pk;
/********************/
void draw(int x,int y)
{
   row=x;
   do
   {
      col=19;
      do
      {
line(col,row,col+40,row);
col=col+80;
}while(col<=619);
      row=row+30;
   }while(row<=y);
}
/********************/
void redraw()
{
   for(i=19;i<=619;i=i+40)
line(i,79,i,169);
   for(i=19;i<=619;i=i+40)
line(i,219,i,309);
   for(i=19;i<=619;i=i+40)
line(i,359,i,449);
   draw(79,169);
   draw(219,309);
   draw(359,449);
   outtextxy(30,30,"Enter 999 to exit...");
}
void start()
{
   detectgraph(&gd,&gm);
   initgraph(&gd,&gm,"c:\\tc\\bgi");
   for(i=0;i<=2;i++)
q[i]=check[i]=999;
   level=0;
   mybrk=0;
   optindex=0;
}

void end()
{
ii=0;
level=level+1;
if(level==3)
{
  level=0;
  cleardevice();
}
}
void print()
{
for(m=0;m<=2;m++)
{
  k=q[m];
  if(k!=999)
  {
     sprintf(aaa,"%d",k);
     outtextxy((ii*80)+10+19,(m*30)+10+79+(140*level),aaa);
  }
}
ii=ii+1;
}
void opt()
{
   cout<<"\n Enter Size of OPT Tape :- ";cin>>optsize;
   cout<<"\n Enter Elements :- ";
   pos=0;
   for(i=0;i<optsize;i++)
cin>>arr[i];
   clrscr();
   getch();
   start();
   for(;;)
   {
      ii=0;
      redraw();
      do
      {
jj=0;pos=0;
k=value=arr[optindex];
sprintf(aaa,"%d",arr[optindex]);
outtextxy(20+(ii*80)+10,60+(139*level),aaa);
for(i=0;i<=2;i++)
  if(q[i]==value)
  {
     outtextxy((ii*80)+10+19,90+10+79+(140*level),"N.F");
     goto A;
  }
outtextxy((ii*80)+10+19,90+10+79+(140*level),"F");
pgfault=pgfault+1;
for(j=0;j<=2;j++)
check[j]=q[j];
for(j=optindex+1;j<optsize;j++)
{
grow[jj]=arr[j];
jj=jj+1;
}
flag1=flag2=flag3=0;
for(j=0;j<jj;j++)
{
if(flag1!=1)
if(check[0]==grow[j])
{
if(j>pos)
{
pos=j;
posval=check[0];
}
flag1=1;
}
if(flag2!=1)
if(check[1]==grow[j])
{
if(j>pos)
{
pos=j;
posval=check[1];
}
flag2=1;
}
if(flag3!=1)
if(check[2]==grow[j])
{
if(j>pos)
{
pos=j;
posval=check[2];
}
flag3=1;
}
}
if(flag1!=1)
{
posval=check[0];
goto Exit;
}
if(flag2!=1)
{
posval=check[1];
goto Exit;
}
if(flag3!=1)
{
posval=check[2];
goto Exit;
}
Exit:
for(j=0;j<3;j++)
if(q[j]==posval)
{
q[j]=value;
goto A;
}
A:
print();
optindex=optindex+1;
if(optindex>optsize)
goto Samapt;
getch();
}while(ii<8);
end();
}
   Samapt:
   sprintf(aaa,"%d",pgfault);
   outtextxy(100,470,"NO. Of Page Faults :- ");
   outtextxy(500,470,"...OPT...");
   outtextxy(300,470,aaa);
   SaveBMP16("c:\\op33.bmp");
   closegraph();
}
/********************/
void fifo()
{
   start();
   for(;;)
   {
      ii=0;
      redraw();
      do
      {
outtextxy(0,0,"   <== Enter Value...");
scanf("%d",&value);
k=value;
if(k==999)
  goto Samapt;
sprintf(aaa,"%d",k);
outtextxy(20+(ii*80)+10,60+(139*level),aaa);
for(i=0;i<=2;i++)
  if(q[i]==value)
  {
     outtextxy((ii*80)+10+19,90+10+79+(140*level),"N.F");
     goto A;
  }
l=mybrk%3;
mybrk=mybrk+1;
q[l]=value;
outtextxy((ii*80)+10+19,90+10+79+(140*level),"F");
pgfault=pgfault+1;
A:
print();
}while(ii<8);
end();
}
   Samapt: getch();
   sprintf(aaa,"%d",pgfault);
   outtextxy(100,470,"NO. Of Page Faults :- ");
   outtextxy(500,470,"...FIFO...");
   outtextxy(300,470,aaa);
   SaveBMP16("c:\\op1.bmp");
   closegraph();
}
/********************/
void lru()
{
   start();
   for(row=0;row<=3;row++)
prio[row]=999;
   for(;;)
   {
      mybrk=0;ii=0;
      redraw();
      do
      {
outtextxy(0,0,"   <== Enter Value...");
scanf("%d",&value);
k=value;
if(k==999)
  goto Samapt;
for(col=0;col<=3;col++)
  temp[col]=prio[col];
prio[0]=value;
j=1;
for(i=0;i<=2;i++)
{
  if(value!=temp[i])
  {
     prio[j]=temp[i];
     j=j+1;
  }
}
sprintf(aaa,"%d",k);
outtextxy(20+(ii*80)+10,60+(139*level),aaa);
for(i=0;i<=2;i++)
  if(q[i]==value)
  {
     outtextxy((ii*80)+10+19,90+10+79+(140*level),"N.F");
     goto A;
  }
for(i=0;i<=3;i++)
{
  if(prio[3]==q[i])
  {
     q[i]=value;
     goto BAHER;
  }
}
BAHER:
outtextxy((ii*80)+10+19,90+10+79+(140*level),"F");
pgfault=pgfault+1;
A:
print();
}while(ii<8);
end();
}
   Samapt: getch();
   sprintf(aaa,"%d",pgfault);
   outtextxy(100,470,"NO. Of Page Faults :- ");
   outtextxy(500,470,"...LRU...");
   outtextxy(300,470,aaa);
   SaveBMP16("c:\\op22.bmp");
   closegraph();
}
/********************/
void main()
{
   clrscr();
   do
   {
   pgfault=0;
   cout<<"\n ********** Menu **********";
   cout<<"\n (1) FIFO (2) LRU (3) OPT (4) Exit";
   cout<<"\n Enter Choice ==> ";
   cin>>ch;
   switch(ch)
   {
case 1: fifo();break;
case 2: lru();SaveBMP16("c:\\op2.bmp");break;
case 3: opt();SaveBMP16("c:\\op3.bmp");break;
case 4: exit(0);break;
   }
   clrscr();
   }while(ch!=4);
}
/********************** Output ************************/



No comments:

Post a Comment