Implementation of LRU Page Replacement Algorithm using C++


//LRU Page Replacement Algorithm

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

#include<stdlib.h>

#define max 100

class lrupage

{

private:

int frame[10], count[10], cstr[max];

int tot,nof,fault;

public:

lrupage(){fault=0;}

void getdata();

void push();

void dis();

};

void lrupage::getdata()

{

int pno,i=0;

cout<<“\n\t\tL R U – Page Replacement Algorithm\n”;

cout<<“\nEnter No. of Page Frames:”;

cin>>nof;

cout<<“\nEnter the Context String:(Press -1 to end)\n”;

do

{

cin>>pno;

cstr[i++]=pno;

}while(pno!=-1);

tot=i-1;

}

void lrupage::push()

{

int x,i,flag=0,nc=0,maximum,maxpos=-1,mark=0;

for(i=0;i<nof;i++)

{

frame[i]=-1;

count[i]=mark–;

}

for(i=0;i<tot;i++)

{

flag=0;

x=cstr[i];

nc++;

for(int j=0; j<nof; j++)

{

for(int k=0; k<nof;k++)

count[k]++;

if(frame[j]==x)

{

flag=1;

count[j]=1;

break;

}

}

if(flag==0)

{

maximum = 0;

for(int k=0;k<nof;k++)

{

if(count[k]>maximum && nc>nof)

{

maximum=count[k];

maxpos = k;

}

}

if(nc>nof)

{

frame[maxpos]=x;

count[maxpos]=1;

}

else

frame[nc-1]=x;

fault++;

cout<<“\nThe Page Fault No. “<<fault<<endl;

dis();

getch();

}

}

cout<<“\nTotal Page Faults :”<<fault;

}

void lrupage::dis()

{

int i=0;

cout<<“\n—–\n”;

while(i<nof)

{

cout<<“|   “<<frame[i]<<”   “;

i++;

}

cout<<“|”;

}

void main()

{

lrupage lru;

clrscr();

lru.getdata();

lru.push();

getch();

}

1 Comment

  1. thanks for ur wonderful comment….

    keep it up…


Sorry, the comment form is closed at this time.

Comments RSS