页面置换算法(FIFO、LRU)用C++实现

一、实验目的

为了使大的作业(其地址空间超过主存可用空间)或多个作业的地址空间之和超过实际主存空间时,仍能运行,引入了虚拟存储器的概念。使运行作业的一部分地址空间在主存,另一部分地址空间在辅存,由操作系统实现多级存储器的自动管理,实现主存空间的自动覆盖。要求学生通过本实验,对请求分页管理有一个清楚的概念。

二、实验原理

(1)在分页虚拟存储系统中,当硬件发出缺页中断后转操作系统处理缺页中断。查主存分块表看有无可用空闲块。若有,则为进程分配-块。如果主存中已无空闲块,当采用先进先出算法时,已进人的各页排成一个FIFO队列,其头部放在变量K中。淘汰时,淘汰最先进人主存的一页。若该页修改过,还要存入磁盘。然后再把当前要访问的页装人该块,并修改页表和存储分块表。恢复中断现场,继续执行被中断的指令。
页面置换算法(FIFO、LRU)用C++实现_第1张图片
数组P中各个元素为作业已在主存的页号。假定作业最多可分配m块,且该作业的装人情况如图10-2所示。当淘汰一页时,总是淘汰P[K]所指页。之后调整数组P:
-----P[K]=要装入的页
-----K=(K+1)mod m
先进先出(FIFO)淘汰算法框图如图10-2所示。
(2)当采用LRU算法时,则淘汰最近很少访问的页。
该算法也可采用一个数组P记录该作业已在主存的页。假定分给该作业4个空闲块。
为了模拟LRU算法,将数组P设计成一个能含有4个元素的堆栈,栈指针用HEAD 表示。假定开始时,堆栈为空。HEAD的初值为0。当调一个页时,按P[HEAD]-调入的页号,HEAD=(HEAD+1) mod m。当访问的页在主存时,将该页调整到栈顶:首先找到该页在P中的位置A.若A≠HEAD,则将该页放栈顶,其他页依次向下移,以反映该页是最近访问的页。
最近很少访问(LRU)淘汰算法框图如图10-3所示。
(3)FIFO和LRU算法使用的指令序列如表10-3所示。
页面置换算法(FIFO、LRU)用C++实现_第2张图片
页面置换算法(FIFO、LRU)用C++实现_第3张图片

三、实验内容

1.FIFO算法

#include 
#include
#include 
#include 
#define N 4//物理块数
using namespace std;
int interrupt_times = 0;//记录缺页中断次数;
int exchange_times = 0;//记录页面置换次数
double short_rate;//缺页率

class Block{
     
    public:
        Block(){
     flag = 0;time = 0;}
        void pushpage(int num){
     //将某一页面装进物理块中
            page_number = num;
            time = 1;
            flag = 1;
        }
        int getpage_number(){
     return page_number;}
        bool getflag(){
     return flag;}
        void addtime(){
      time++; }
        int gettime(){
     return time;}
    private:
        int page_number;//页面号
        bool flag;      //是否是空物理块
        int time;       //页面在物理块中的时间
};

int ExistEmpty(Block *block){
     
    for (int i = 0; i < N; i++)
        if(block[i].getflag()==0)
            return i;
    return -1;
}
bool ExistPage(Block *block,int page){
     
    for (int i = 0; i < N;i++){
     
        if(block[i].getpage_number()==page)
            return true;
    }
    return false;
}

bool cmp(Block b1,Block b2){
     
    return b1.gettime()>b2.gettime();
}

void PrintBlock(Block *block){
     
    for (int j = 0; j < N;j++){
     
        if(block[j].getflag())
            cout << "  |"<<block[j].getpage_number() <<"|"<< endl;
        else
            cout << "  | |" << endl;
    }
    cout << "   " << endl;
}
void FIFO(Block *block,vector<int> page){
     
    for (int i = 0; i < page.size();i++){
     
        if(ExistEmpty(block)>=0){
     
            block[ExistEmpty(block)].pushpage(page[i]);
            for (int j = 0; j < ExistEmpty(block);j++)
                block[j].addtime();
            interrupt_times++;
            cout << page[i] << endl;
            PrintBlock(block);
        }
        else if(ExistPage(block, page[i])){
     
            for (int j = 0; j < N;j++)
                block[j].addtime();
            cout << page[i] << endl << endl<<endl;
        }
        else{
     
            sort(block, block + N, cmp);
            block->pushpage(page[i]);
            for (int j = 1; j < N;j++)
                block[j].addtime();
            interrupt_times++;
            exchange_times++;
            cout << page[i] << endl;
            PrintBlock(block);
        }
    }
    short_rate = (interrupt_times*1.0 / page.size());
    cout << "The times of interrupting is " << interrupt_times <<"."<< endl;
    cout << "The times of exchanging is " << exchange_times <<"."<< endl;
    cout <<"The rate of the missing page is "<< short_rate <<"."<< endl;
}

int main(){
     
    Block block[N];
    int n[12] = {
     4,3,2,1,4,3,5,4,3,2,1,5};
    vector<int> page(n,n+12);
    FIFO(block, page);

    return 0;
}

页面置换算法(FIFO、LRU)用C++实现_第4张图片

2.LRU算法

#include 
#include
#include 
#include 
#define N 3//物理块数
using namespace std;
int interrupt_times = 0;//记录缺页中断次数;
int exchange_times = 0;//记录页面置换次数
double short_rate;//缺页率

class Block{
     
    public:
        Block(){
     flag = 0;time = 0;}
        void pushpage(int num){
     //将某一页面装进物理块中
            page_number = num;
            time = 1;
            flag = 1;
        }
        int getpage_number(){
     return page_number;}
        bool getflag(){
     return flag;}
        void addtime(){
      time++; }
        void settime() {
      time = 1; }
        int gettime(){
     return time;}
    private:
        int page_number;//页面号
        bool flag;      //是否是空物理块
        int time;       //页面在物理块中的时间
};

int ExistEmpty(Block *block){
     
    for (int i = 0; i < N; i++)
        if(block[i].getflag()==0)
            return i;
    return -1;
}
int ExistPage(Block *block,int page){
     
    for (int i = 0; i < N;i++){
     
        if(block[i].getpage_number()==page)
            return i;
    }
    return -1;
}

bool cmp(Block b1,Block b2){
     
    return b1.gettime()>b2.gettime();
}

void PrintBlock(Block *block){
     
    for (int j = 0; j < N;j++){
     
        if(block[j].getflag())
            cout << "  |"<<block[j].getpage_number() <<"|"<< endl;
        else
            cout << "  | |" << endl;
    }
}
void LRU(Block *block,vector<int> page){
     
    for (int i = 0; i < page.size();i++){
     
        if(ExistEmpty(block)>=0){
     
            block[ExistEmpty(block)].pushpage(page[i]);
            for (int j = 0; j < ExistEmpty(block);j++)
                block[j].addtime();
            interrupt_times++;
            cout << page[i] << endl;
            PrintBlock(block);
        }
        else if(ExistPage(block, page[i])>=0){
     
            block[ExistPage(block, page[i])].settime();
            for (int j = 0; j < N;j++)
                if(j!=ExistPage(block, page[i]))
                    block[j].addtime();
            cout << page[i] << endl << endl;
        }
        else{
     
            sort(block, block + N, cmp);
            block->pushpage(page[i]);
            for (int j = 1; j < N;j++)
                block[j].addtime();
            interrupt_times++;
            exchange_times++;
            cout << page[i] << endl;
            PrintBlock(block);
        }
    }
    short_rate = (interrupt_times*1.0 / page.size());
    cout << "The times of interrupting is " << interrupt_times << endl;
    cout << "The times of exchange is " << exchange_times << endl;
    cout <<"The rate of the missing page is "<< short_rate << endl;
}

int main(){
     
    Block block[N];
    int n[20] = {
     7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
    vector<int> page(n,n+20);
    LRU(block, page);

    return 0;
}

页面置换算法(FIFO、LRU)用C++实现_第5张图片

你可能感兴趣的:(笔记)