Operating system: using c ++ to implement FIFO (First In First Out) and LRU (least recently use) page replacement

On the following algorithm: First of similar books initialized to empty, where an empty physical block using # instead of each physical block first, because, within the limits of the number of physical blocks, all pages can be in memory, so no need to use a start algorithm, all direct investment.

Then the FIFO: y a value directly defined, each placed in a page, y ++, and mod (the total number of physical blocks), where y is similar to a loop pointer.

In the LRU: define a value of y, but not y pointer is cyclic, but each time a page is detected, when the page fault condition occurs, it first calculates a least recently used page in which the physical block, then the physical block the index assigns the value of y, finally displacing the physical block is located in the index page.

#include<iostream>

#include<string>

using namespace std;

 

void FIFO(int b,string a);

void LRU(int b,string a);

int main ()

{

       string a;

       int b;

       cout << "Please enter the number of physical blocks:";

       cin>>b;

       cout << "Please enter the page number quoted strings:";

       cin>>a;

       cout << "replacement process using FIFO page replacement algorithm is as follows:" << endl;

    FIFO(b,a);

       cout<<"*********************************************"<<endl;

       cout << "using LRU page replacement algorithm replacement process is as follows:" << endl;

       LRU(b,a);

       return 0;

}

void FIFO(int b,string a)

{

       char m [10] = { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}; // the contents of the initialization physical block number

       int i = 0, j, x, y = 0, count1 = 0; // y is replaced labeled, count1 marked as missing pages

       do{

              if(i<b)

              {

                     m [y] = a [i]; // missing page after page write

                     i++;

                     count1 ++; // missing pages marked ++

                     y = (y + 1)% b; // replaces the marker ++

                     cout<<a[i-1]<<" ";

                     for (j = 0; j <b; j ++) // Print

                            cout<<m[j];

                  cout<<endl;

                     continue;

              }

              for (j = 0; j <b; j ++) // determines whether the page fault

              {

                     if (a [i] == m [j]) // assignment x will not page fault 1

                     {

                            x=1;

                            break;

                     }

              }

              if (x == 1) // No page fault processing

              {

                     i++;

                     x=0;

                     cout<<a[i-1]<<" "<<endl;

                     continue;

              }

              else // page fault processing

              {

                     m[y]=a[i];

                     i++;

                     y=(y+1)%b;

                     cout<<a[i-1]<<" ";

                     for (j = 0; j <b; j ++) // Print

                            cout<<m[j];

                  cout<<endl;

            count1++;

              }

       }while(a[i]!='#');

       cout << "number of pages missing page" << "" << "number of page replacement" << endl;

       cout<<"     "<<count1<<"           "<<count1-b<<endl;;

       / * Print the replacement number of times and missing pages * /

}

void LRU(int b,string a)

{

       char m[10]={'#','#','#','#','#','#','#','#','#','#'};

       int i = 0, j, x, y = 0, count1 = 0, min = 10000; // min to an initial value of the most recently used

       cout << "replacement process is as follows" << endl;

       do{

              if(i<b)

              {

                     m [y] = a [i]; // missing page after page write

                     i++;

                     count1 ++; // missing pages marked ++

                     y = y + 1; // tag substitution ++

                     cout<<a[i-1]<<" ";

                     for (j = 0; j <b; j ++) // Print

                            cout<<m[j];

                  cout<<endl;

                     continue;

              }

              for (j = 0; j <b; j ++) // determines whether the page fault

              {

                     if(a[i]==m[j])

                     {

                            x=1;

                            break;

                     }

              }

              if (x == 1) // not only deal with missing pages

              {

                     i++;

                     x=0;

                     cout<<a[i-1]<<" "<<endl;

 

                     continue;

              }

              else // page fault processing

              {

                     for (j = 0; j <b; j ++) // find the least recently used page

                     {

                            for(int t=i-1;t>=0;t--)

                            {

                                   if(m[j]==a[t])

                        break;

                            }

                            if(t<min)

                            {

                                   min = t;

                                   y = j;

                            }

                     }

                     m [y] = a [i]; // least recently used page replacement

                     min = 10000; // initializes the min

                     i++;

                     cout<<a[i-1]<<" ";

                     for (j = 0; j <b; j ++) // Print

                            cout<<m[j];

                  cout<<endl;

            count1++;

              }

       }while(a[i]!='#');

       cout << "number of pages missing page" << "" << "number of page replacement" << endl;

       cout<<"     "<<count1<<"           "<<count1-b<<endl;

       / * Print page missing page number and the number of page replacement * /

}

// test data 70120304230321201701 #

 

Guess you like

Origin www.cnblogs.com/RiverChen/p/10963122.html