LRU页面置换算法原理及Java代码的实现

    LRU(Least Recently Used)算法,也叫:最近最少使用置换法。顾名思义,就是当需要置换一页时,在内存块中找到一个最久没有使用的页面予以淘汰并进行置换的算法。这是一种时间向前看的情况。

    为了更形象地说明这种情况,我们先来举一个例子:

假设现在有一个请求队列:7、0、1、2、0、3、0、4,总共有8个数,内存块容量为3个。


接下来我们就来讲下LRU算法的基本置换过程:

    第一步:


先把所有的内存块装满。我们先设请求块的数组为a[ ],则a[0]=7,a[1]=0,a[2]=1,a[3]=2 ... ...    此时,a[3]=2在内存块中不存在,这时候就要进行页面置换。

    第二步:从a[3]往前看,找到time=3这个最大时间值对应的内存块为:7,表示内容为“”7“”的内存块停留的时间最久,需要替换掉。此时的内存块就如下图所示:


如果请求块的当前数据在内存块中已存在,则跳过;如果不存在,则进行页面置换。置换的后面步骤以此类推。置换结束后的最终结果如下:


所以,依次发生置换的页面号是:7、0、1、2、3、4 ,缺页数为:5


具体实现的代码如下:

public class LRU
{

public static int countCacheMiss(int max_cache_size , int[] page_requests) 
{
boolean exist_flag = false;//检查是否在缓存中存在
int memory_cache[] = new int[max_cache_size];//内存块
int requests_page_number = page_requests.length;//请求页的页数
int time[] = new int[3];//时间计数值
int time_tmp=0;//记录时间值
int change_page_number = 0;//缺页数

System.out.print("置换页面:");

for(int i=0;i<max_cache_size;i++)//先把内存块装满
{
change_page_number++;
memory_cache[i] = page_requests[i];
System.out.print(memory_cache[i]+"   ");
}

for(int i=max_cache_size;i<requests_page_number-1;i++)//剩下的请求页
{
int time_max=0;
exist_flag = false;
for(int j=0;j<max_cache_size;j++)//判断当前请求页是否在内存块中存在
{
if( memory_cache[j] == page_requests[i] ) exist_flag=true;
}
if(exist_flag)continue;
else //如果缺页,就置换页面
{
change_page_number++;
for(int j=0;j<max_cache_size;j++)//遍历每一个内存块
{
for(int k=i-1;k>=0;k--)//在page_requests请求页中,[i-1]->[0] 依次往前找
{
if(memory_cache[j] != page_requests[k])time_tmp++;
else
{
time[j]=time_tmp;//把查询到的time时间值存放到记录数组中
break;
}
}
time_tmp=0;
}
for(int p=0;p<max_cache_size;p++)//找到内存块中最大的时间值,记为max
{
if(time[p] > time_max)time_max = time[p];
}
int p=0;
while(time_max != time[p])//找到最大时间值处于数组中的位置
{
p++;
}
memory_cache[p] = page_requests[i];//对内存块中改位的页面进行置换
System.out.print("  "+memory_cache[p]);
}
}
return change_page_number;
}


public static void main(String[] args) 
{
int result=0;
int a[]={7,0,1,2,0,3,0,4};
result = countCacheMiss(3,a);
System.out.println("");
System.out.println("缺页数:"+result);
}
}
{

public static int countCacheMiss(int max_cache_size , int[] page_requests) 
{
boolean exist_flag = false;//检查是否在缓存中存在
int memory_cache[] = new int[max_cache_size];//内存块
int requests_page_number = page_requests.length;//请求页的页数
int time[] = new int[3];//时间计数值
int time_tmp=0;//记录时间值
int change_page_number = 0;//缺页数

System.out.print("置换页面:");

for(int i=0;i<max_cache_size;i++)//先把内存块装满
{
change_page_number++;
memory_cache[i] = page_requests[i];
System.out.print(memory_cache[i]+"   ");
}

for(int i=max_cache_size;i<requests_page_number-1;i++)//剩下的请求页
{
int time_max=0;
exist_flag = false;
for(int j=0;j<max_cache_size;j++)//判断当前请求页是否在内存块中存在
{
if( memory_cache[j] == page_requests[i] ) exist_flag=true;
}
if(exist_flag)continue;
else //如果缺页,就置换页面
{
change_page_number++;
for(int j=0;j<max_cache_size;j++)//遍历每一个内存块
{
for(int k=i-1;k>=0;k--)//在page_requests请求页中,[i-1]->[0] 依次往前找
{
if(memory_cache[j] != page_requests[k])time_tmp++;
else
{
time[j]=time_tmp;//把查询到的time时间值存放到记录数组中
break;
}
}
time_tmp=0;
}
for(int p=0;p<max_cache_size;p++)//找到内存块中最大的时间值,记为max
{
if(time[p] > time_max)time_max = time[p];
}
int p=0;
while(time_max != time[p])//找到最大时间值处于数组中的位置
{
p++;
}
memory_cache[p] = page_requests[i];//对内存块中改位的页面进行置换
System.out.print("  "+memory_cache[p]);
}
}
return change_page_number;
}


public static void main(String[] args) 
{
int result=0;
int a[]={7,0,1,2,0,3,0,4};
result = countCacheMiss(3,a);
System.out.println("");
System.out.println("缺页数:"+result);
}
}


最终程序的输出结果如下:


以上若有错误,欢迎各位批评指出。或者如果有更好的算法实现,也可以提出,大家可以互相学习交流。

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值