1. 程式人生 > >頁面排程演算法 FIFO,LRU,OPT,及C++程式碼

頁面排程演算法 FIFO,LRU,OPT,及C++程式碼

頁面排程演算法 FIFO,LRU,OPT

介紹了三種頁面排程演算法,給出了C++程式碼

1.FIFO

先來先去演算法這個非常好理解,給出分析圖
這裡寫圖片描述
可以看出,缺頁次數為8次,缺頁中斷率為8/12=66.7%,依次置換的頁面為:1,2,5,3,4

C++程式碼在最後給出

2.LRU

LRU,Least Recently Used 近期最少使用演算法,先給出分析圖
這裡寫圖片描述

這個也不難理解,我們只需要從之前的一個頁面開始往左尋找就可以了,比如在第一次遇到頁面3時,我們發現3左邊是1,再左邊是5,所以1,5近期使用了,就把1,5留下,另外一個頁面置換掉,所以此時記憶體中的頁面2就被置換了。

C++程式碼在最後給出

3.OPT

最佳置換演算法,是一種理論演算法,對未來的頁面檢測從而判斷置換頁。分析圖如下
這裡寫圖片描述

OPT跟LRU非常相似,LRU是往左找,OPT就是往右找啦,比如第一次遇到頁面3時,往右依次是2,5,所以2,5頁面保留,把記憶體中的頁面1置換掉。

4. 三種演算法的C++程式碼

FIFO:

//FILENAME: FIFO.cpp
#include<iostream>
using namespace std;

int main()
{
    int memory[3]={-1,-1,-1}; //-1 means no page in this memory page
    int page[12]={1,2,5,1,3,2,5,4,1,4,5,2};//the test pages
    int pm=0; //pointer of memory
    int count=0;//missing page count
    int replace[12];
    int pr=0;

    cout<<"######FIFO#######"<<endl;
    //search begin
    for (int i=0;i<12;i++)
    {
        //check if there is page[i] in memory
        bool exist = false;
        for (int j=0;j<3;j++)
        {
            if (page[i]==memory[j])
            {
                exist=true;
                break;
            }
        }
        //not exist , replace this memory page
        if (exist==false)
        {
            if(memory[pm]!=-1)
            {
                replace[pr]=memory[pm];
                pr++;
            }

            count++;
            memory[pm]=page[i];
            pm++;
            if (pm==3) pm=0;
        }
        //output
        cout<<page[i]<<":  [ ";
        for(int j=0;j<3;j++)
        {
            if (memory[j]==-1) cout<<"*  ";
            else cout<<memory[j]<<"  ";
        }
        cout<<"]"<<endl;
    }
    //output
    cout<<"######################"<<endl;
    cout<<"the lack page count = " <<count<<endl;

    cout<<"repalce pages are  : ";
    for (int i=0;i<pr;i++)
    {
        cout<<replace[i]<<" ";
    }
    cout<<endl;

    cout<<"the rate of page lack is "<<count/12.0*100<<"%"<<endl;

    return 0;
}

LRU:

//FILENAME: LRU.cpp
#include<iostream>
using namespace std;

int main()
{
    int memory[3]={-1,-1,-1};
    int page[12]={1,2,5,1,3,2,5,4,1,4,5,2};//the test pages
    int count=0;//lack page count
    int replace[12]; //replace page record
    int pr=0;    //pointer of replace

    cout<<"######LRU#######"<<endl;
    //search begin
    for (int i=0;i<12;i++)
    {
        //there are 3 memory pages in memory ,so if i<3,just put it in memory
        if(i<3)
        {
            memory[i]=page[i];
            count++;
        }
        else
        {
            //check if this page is in memory already
            bool exist=false;
            for(int j=0;j<3;j++)
            {
                if(page[i]==memory[j])
                {
                    exist=true;
                    break;
                }
            }

            if (exist==false)
            {
                //begin to choose a memory page to replace
                int last=0;
                bool ok[3];
                for (int j=0;j<3;j++) ok[j]=false;

                //check from i step -1 till 0
                for(int j=i;j>=0;j--)
                {
                    for(int k=0;k<3;k++)
                    {
                        if (page[j]==memory[k])
                        {
                            ok[k]=true;
                            last++;
                            break;
                        }
                    }
                    if (last==2)break;
                }
                //check which ok ==false
                for (int j=0;j<3;j++)
                {
                    if (ok[j]==false)
                    {
                        //replace this memory[j]
                        count++;
                        replace[pr]=memory[j];
                        pr++;
                        memory[j]=page[i];
                        break;
                    }
                }

            }


        }

        //output
        cout<<page[i]<<":  [ ";
        for(int j=0;j<3;j++)
        {
            if (memory[j]==-1) cout<<"*  ";
            else cout<<memory[j]<<"  ";
        }
        cout<<"]"<<endl;
    }

    //out put
    cout<<"######################"<<endl;
    cout<<"the lack page count = " <<count<<endl;
    cout<<"repalce pages are  : ";
    for (int i=0;i<pr;i++)
    {
        cout<<replace[i]<<" ";
    }
    cout<<endl;
    cout<<"the rate of page lack is "<<count/12.0*100<<"%"<<endl;
    return 0;
}

OPT:

//FILENAME: OPT.cpp
#include<iostream>
using namespace std;

int main()
{
    int memory[3]={-1,-1,-1};
    int page[12]={1,2,5,1,3,2,5,4,1,4,5,2};//the test pages
    int count=0;//missing page count
    int replace[12]; //replace page record
    int pr=0;  // pointer of replace

    cout<<"######OPT#######"<<endl;
    //search begin
    for (int i=0;i<12;i++)
    {
        //there are 3 memory pages in memory ,so if i<3,just put it in memory
        if(i<3)
        {
            memory[i]=page[i];
            count++;
        }
        else
        {
            //check if this page is in memory already
            bool exist=false;
            for(int j=0;j<3;j++)
            {
                if(page[i]==memory[j])
                {
                    exist=true;
                    break;
                }
            }

            if (exist==false)
            {
                //###############################
                //begin to choose a memory page to replace
                int later=0;
                bool ok[3];
                for (int j=0;j<3;j++) ok[j]=false;

                //check from i step -1 till 0
                for(int j=i+1;j<12;j++)
                {
                    for(int k=0;k<3;k++)
                    {
                        if (page[j]==memory[k])
                        {
                            ok[k]=true;
                            later++;
                            break;
                        }
                    }
                    if (later==2)break;
                }
                //check which ok ==false
                for (int j=0;j<3;j++)
                {
                    if (ok[j]==false)
                    {
                        //replace this memory[j]
                        count++;
                        replace[pr]=memory[j];
                        pr++;
                        memory[j]=page[i];
                        break;
                    }
                }
                //#############################

            }


        }


        //output
        cout<<page[i]<<":  [ ";
        for(int j=0;j<3;j++)
        {
            if (memory[j]==-1) cout<<"*  ";
            else cout<<memory[j]<<"  ";
        }
        cout<<"]"<<endl;
    }
    cout<<"######################"<<endl;
    cout<<"the lack page count = " <<count<<endl;

    cout<<"repalce pages are  : ";
    for (int i=0;i<pr;i++)
    {
        cout<<replace[i]<<" ";
    }
    cout<<endl;

    cout<<"the rate of page lack is "<<count/12.0*100<<"%"<<endl;

    return 0;
}