FIFO page replacement algorithm simulation

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/saber_wtq/article/details/84068461

FIFO page replacement algorithm simulation

Time limit: 1 Sec Memory Limit: 128 MB
 

Title Description

First, the purpose of the experiment
1, a deeper understanding FIFO page replacement algorithm and related concepts.
2, grasp the displacement map (p175 textbook Figure 5-3) Description page replacement algorithm.
3, calculates the page fault rate.

Second, the principle of the experiment
the number of page 1, the process is often much larger than the page frames assigned to the operating system the process (physical block) number, therefore, often only part of the page can be loaded into the page frame. 
2, when the process calls a page, the page is not likely to occur page box, which requires an algorithm to determine which page a page out of the box, in order to load a new page to the page call box. 
3, FIFO page replacement algorithm stay out of the longest in main memory page, on the grounds that: the first is loaded into the Page box, maximum likelihood it is no longer accessible. 

Third, the experimental requirements
1, with the C ++ programming language, analog FIFO page replacement algorithm.
2, main functions and part of the code you have written, you only need to write a function fifo and submit (submit only fifo function implementation code).

#include <the iostream>
#include <The iomanip>
#include <Vector>
the using namespace STD;

/////////////////////////////// /////////////////////////////////////////////////
/ / fifo function declaration
// input parameters:
// pageframeNum: the operating system assigned to the page frame number of a process;
// pageCallSequence: page calling sequences, each sequence is the page number of the calling page.
////////////////////////////////////////////////// //////////////////////////////
void FIFO (int pageframeNum, Vector <int> pageCallSequence &);

int main ()
{
    int I , pageframeNum, n-;

    CIN >> pageframeNum; // number of page frames assigned to the input of a process (called "page frames" as the textbook: physical block)
    CIN n->>; // length of the input sequence of the process page calls
    vector <int> pageCallSequence (n) ; // call to define the page sequence, the sequence is invoked with every page in page number
    for (i = 0; i < n; i ++) // number of pages n to construct page calling sequence
    {
        CIN >> pageCallSequence [I];
    }

    fifo (pageframeNum, pageCallSequence); // analog FIFO page replacement algorithm

    return 0;


. 3, in the fifo function, implemented: each visit page, by page page frame number of the order of the output page frame number; calculating and outputting a page fault rate. 
4, the input and output format, see Sample Input Sample Output. In the sample output: but the last line, each line is the page number in accordance with the order of the page frame number of the output page box, if not already loaded page to the page frame, the output 1; the last line is missing page rate, retention three significant figures. 
 

 

Entry

3 12
4 3 2 1 4 3 5 4 3 2 1 5

 

Export

4,-1,-1
4,3,-1
4,3,2
1,3,2
1,4,2
1,4,3
5,4,3
5,4,3
5,4,3
5,2,3
5,2,1
5,2,1
0.750

 

Sample input

5 20
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

 

Sample Output

7,-1,-1,-1,-1
7,0,-1,-1,-1
7,0,1,-1,-1
7,0,1,2,-1
7,0,1,2,-1
7,0,1,2,3
7,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,0,1,2,3
4,7,1,2,3
4,7,0,2,3
4,7,0,1,3
0.450

It is a pure analog, nothing more difficult

code show as below

#include<iostream>
#include<vector>
using namespace std;

FIFO void (int pageframeNum, Vector <int> & pageCallSequence)
{
    int I, J, K;
    const int pageCallSequence.size M = ();
    const int N = pageframeNum;
    BOOL T = 0; // flag
    int * a = new int [N]; // dynamic array
    a float S;
    for (I = 0; I <N; I ++)
    {
        A [I] = -1;
    }
    K = 0;
    for (J = 0; J <M; J ++)
    {
        T = 0; the default page is not in memory before loading the new page //
        for (I = 0; I <N; I ++)
            IF (pageCallSequence [J] == a [I]) // if the page has been invoked flag stored in memory location 1, otherwise set to 0
            {
                T = 1;
                BREAK;
            }
        if (T == 1) // if the page is already in memory, the output of the current memory page number
        {
            for (I = 0; I <N; I ++)
            {
                IF (== N-I. 1)
                    the printf ( " D% ", a [I]);
                the else
                    the printf ("% D, ", a [I]);
            }        
        }
        the else // If the page is not in memory, then replaced into the page memory longest
        {
            a [ k% N] = pageCallSequence [j ]; // FIFO implemented code
            k ++; // record the number of page replacement
            for (I = 0; I <N; I ++)
            { 
                IF (I N == -. 1)
                    the printf ( "% d", a [ i]);
                else
                    printf("%d,", a[i]);
            }
        }
        printf("\n");
    }
    s =1.0*k/M;
    printf("%0.3f\n", s);
    delete a;
}

int main()
{
    int i, pageframeNum, n;

    cin >> pageframeNum; // number of page frames assigned to the input of a process (called "page frames" as the textbook: physical block)
    CIN n->>; // call to the input page of the process sequence length
    vector <int> pageCallSequence (n); // call to define the page sequence, the sequence is invoked with every page in page number
    for (i = 0; i < n; i ++) // number of pages n to construct the page call sequence
    {
        CIN pageCallSequence >> [I];
    }
    FIFO (pageframeNum, pageCallSequence); // analog FIFO page replacement algorithm
    // System ( "PAUSE");
    return 0;
}


 

Guess you like

Origin blog.csdn.net/saber_wtq/article/details/84068461