LRU页面调度算法

来源:互联网 发布:mysql手册中文版下载 编辑:程序博客网 时间:2024/05/18 03:45

//LRU页面调度算法,原理是踢出最近最少使用的置换算法

//该算法淘汰最近那一段时间较久未被访问的那一页

//实现该核心算法代码:

int GetMax(Page *b){//此代码是获取最长时间停留在内存中的页面
     int max = -1;
     int tag = 0;
     for(int i = 0;i< M;i++){
            if(b[i].time > max){
                max = b[i].time;
                tag = i;
            }
     }
     return tag;


}

//LRU核心算法原理:

void Lru(int fold,Page *b){
       int val;
       val = Equation(fold ,b);
       if(val >= 0){
          b[val].time = 0;
          for(int i = 0;i< M ;i++){
                if(i != val )
                     b[i].time++;
 }
 }else{
                       queue[++K] = fold;
                       val = GetMax(b);
                       b[val].num = fold ;
                       b[val].time = 0;
                       for(int i = 0;i<M;i++){
                            if(i != val){
                                b[i].time++;
                            }
                       }
          }
}

//此时算法有两个判断,首先是判断如果在内存中已经存在了此页面,那么假如val>=0来表明已存在,然后表明调用此页面就要将其时间重新标为0了,那么其他内存块的页面又多呆了时间用吧b[i].time++来表示。else的话我们记录这个页面在queue[++k]中,重新通过获取最长时间的那个界面将其踢出去,此时这个新的页面的停留时间则为0.同样其他的页面的时间就的自加了。

我的代码:

输入模拟页面串:保存在a[N]这个数组中={1,0,1,0,2,4,1,0,0,8,7,5,4,3,2,3,4};

此代码为:#include <iostream>
#include <conio.h>
#define M 4
#define N 17
#define Myprintf printf("|-----------------------------------|\n")
using namespace std;
typedef struct page{
        int num;
        int time;
}Page;
Page b[M];
int c[M][N];
int queue[100];
int K;
void Init(Page *b, int c[M][N]){
       int i;
       int j;
       for(i = 0;i< M;i++){
             b[i].num = -1;
             b[i].time  =N-1-i;
       }
       for(i = 0;i<M;i++){
           for(j = 0;j<N;j++){
               c[i][j] = -1;
           }
       }
}


int GetMax(Page *b){
     int max = -1;
     int tag = 0;
     for(int i = 0;i< M;i++){
            if(b[i].time > max){
                max = b[i].time;
                tag = i;
            }
     }
     return tag;


}


int Equation(int fold ,Page *b){
      for(int i = 0;i < M;i++){
          if(fold == b[i].num){
               return i;
          }
      }
       return -1;


}


void Lru(int fold,Page *b){
       int val;
       val = Equation(fold ,b);
       if(val >= 0){
          b[val].time = 0;
          for(int i = 0;i< M ;i++){
                if(i != val )
                     b[i].time++;
 }
 }else{
                       queue[++K] = fold;
                       val = GetMax(b);
                       b[val].num = fold ;
                       b[val].time = 0;
                       for(int i = 0;i<M;i++){
                            if(i != val){
                                b[i].time++;
                            }
                       }
          }
}
int main(){
int i;
     int a[N] = {1,0,1,0,2,4,1,0,0,8,7,5,4,3,2,3,4};
start:
     K = -1;
     Init(b,c);
     for( i = 0 ;i < N;i++){
          Lru(a[i],b);
         // c[0][i] = a[i] ;
          for(int j = 0 ;j < M ;j++){
                c[j][i] = b[j].num;
          }


     }
     cout<<"内存状态为:\n";
     Myprintf;
     for(int j = 0;j < N;j++){
         cout<<a[j]<<" ";
     }
     cout<<endl;
     Myprintf;
     for( i = 0 ;i< M ;i++){
           for(int j = 0;j < N;j++){
               if(c[i][j] == -1){
//  cout<<c[i][j];
                   printf("%2c",32);
               }else{
                    cout<<c[i][j]<<" ";
               }
              // cout<<endl;
           }
       cout<<endl;
     }
     Myprintf;
     cout<<endl;
     cout<<"调入队列为:";
     for( i = 0;i < K +1;i++){
            cout<< queue[i];


     }
     cout<<endl;
     cout<<"缺页的次数为:"<<(K+1)<<endl;
cout<<"缺页频率:"<<(float)(K+1)/N<<endl;
     cout<<"Are you continue! \t n/y?"<<endl;
     if(getche() == 'y'){
         goto start;
     }
     return 0;
}


//此代码仅供遇到同样问题的好友一起学习,希望能快速知其原理,能帮到你们。




































0 0
原创粉丝点击