Write a C++ program to implement LRU algorithm.


C++Program to implement LRU algorithm
//  main.cpp
//  quicksort
//

#include<iostream>
#define max 100

class lrupagereplacement

{
   
   private:
   
    int frame[10], count[10], cstr[max];
   
    int tot,nof,fault;
   
   public:
   
 lrupagereplacement (){fault=0;}
   
    void getdata();
   
    void push();
   
    void dis();
   
};

void lrupagereplacement::getdata()

{
   
    int pno,i=0;
   
   
    std::cout<<"\n Enter No. of Page Frames:";
   
    std::cin>>nof;
   
    std::cout<<"\n Enter the Context String:(Press -1 to end)\n";
   
    do
       
    {
       
        std::cin>>pno;
       
        cstr[i++]=pno;
       
    }while(pno!=-1);
   
    tot=i-1;
   
}

void lrupagereplacement::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++;
           
            std::cout<<"\nThe Page Fault No:"<<fault;
           
            dis();
           
        }
       
    }
   
    std::cout<<"\nTotal Page Faults :"<<fault;
   
}

void lrupagereplacement::dis()

{
   
    int i=0;
   
    std::cout<<"\n-\n";
   
    while(i<nof)
       
    {
       
        std::cout<<"|   "<<frame[i]<<"   ";
       
        i++;
       
    }
   
    std::cout<<"|";
}

int main()

{
   
 lrupagereplacement lru;
   
 lrupagereplacement.getdata();
   
 lrupagereplacement.push();
   
 return 0;
}

SAMPLE OUTPUT
Enter No. of Page Frames:4

Enter the Context String:(Press -1 to end)
1 2 3 4 2 5 9 -1

The Page Fault No1
-
|   1   |   -1   |   -1   |   -1   |
The Page Fault No2
-
|   1   |   2   |   -1   |   -1   |
The Page Fault No3
-
|   1   |   2   |   3   |   -1   |
The Page Fault No4
-
|   1   |   2   |   3   |   4   |
The Page Fault No5
-
|   5   |   2   |   3   |   4   |
The Page Fault No6
-
|   5   |   2   |   9   |   4   |
Total Page Faults :6Program ended with exit code: 0

0 comments:

Feel free to contact the admin for any suggestions and help.