cache 调度算法LRU模拟程序

/*++

Copyright (c) 2007 YourCompany

Module Name:

    <new>

Abstract:

     结合数据结构相关知识使用LRU的策略,对一组访问序列进行内部的Cache更
 新LRU置换算法是选择最近最久未使用的页面予以置换。该算法赋予每个页面一个
 访问字段,用来记录一个页面自上次被访问以来经历的时间T,当须淘汰一个页面
 时,选择现有页面中T值最大的,即最近最久没有访问的页面。这是一个比较合理
 的置换算法。

Author:

    YourName (YourEmail) 2007-06-12

Revision History:

--
*/

//

#include
< iostream.h >



// #define MAX_LEN  20
// #define MAX  5
#define  true 1
#define  false 0

// 定义全局变量
typedef  struct {
        
int state;        //块状态true满,false为空
        int value;        //块内的页面号
        int count;         //块计数器标志块据现在的时间    
    }
Cache;
Cache cache[]
= {{false,-1,0},      //初始化块信息
                {false,-1,0},
                
{false,-1,0},
                
{false,-1,0}}
;    // cache中有四块
    
int  walk_sort[] = {1,1,2,4,3,5,2,1,6,7,1,3} ;   // 页面访问序列

int  m = 4 ;   // cache 块的个数
int  n = 12 ;   // 访问序列的长度
// int n=strlen(walk_sort);   // 页面置换访问序列的个数??????? strlen 用法??????????????


//
void  delay();  // 声明

//  cache更新算法,LRU
void  up_cache(Cache cache[], int  walk_sort[])
{
    
int i=0;  //i为访问序列数组的下标
    int x;
    
int k;
    
int kk;
    
//n=strlen(walk_sort);    //??????????  strlen 和数组的类型有关吗?
    while(i<n)
    
{
        
int j=0;  //J为cache块的下标
        
//满么?
        while(j<m) //当块没有装满情况 
        {
            
if((cache[j].state==false)&&(walk_sort[i]!=cache[j].value))  //装入一块
            {
                cout
<<"cache有空闲块,不考虑是否要置换."<<endl;
                cout
<<walk_sort[i]<<"被调入cache...."<<endl;
                cache[j].value
=walk_sort[i++];
                cache[j].state
=true;
                cache[j].count
=0;
                
int kk=0;
                
            
                
for (x=0;x<m;x++)
                cout
<<"cache块"<<x<<""<<cache[x].value<<endl;
                cout
<<endl;
                
                
//更新其它cache块没使用时间
                while(kk<m)
                
{
                    
if(kk!=j&&cache[kk].value!=(-1))
                    cache[kk].count
++;
                    kk
++;
                }

                delay();
                
break;
            }

            
           
            
if(cache[j].value==walk_sort[i])   //命中
            {
                cout
<<endl;
                cout
<<walk_sort[i]<<"命中!!!"<<endl;
                
for (x=0;x<m;x++)
                cout
<<"cache块"<<x<<""<<cache[x].value<<endl;
                cout
<<endl;
                
int kk=0;
                i
++;
                cache[j].count
=0;
                
//更新其它cache块没使用时间
                while(kk<m)
                    
{
                        
if(kk!=j&&cache[kk].value!=(-1))
                        cache[kk].count
++;
                        kk
++;
                    }

            }

            
            j
++;  //块下标加一
        }

        
        
if(j==m)   //考虑cache块已经满的情况
        {
            cout
<<"cache已经满了,考虑是否置换."<<endl;
            cout
<<endl;
            
int k=0//块下标
            while(k<m)   //遍历块看其中是否有和访问序列相同的页号,有相同的则命中
            {
                
if(cache[k].value==walk_sort[i])
                
{
                    cout
<<endl;
                    cout
<<walk_sort[i]<<"命中!!!"<<endl;
                    
for (x=0;x<m;x++)                          
                    cout
<<"cache块"<<x<<""<<cache[x].value<<endl; //输出各块值
                    i++;
                    cache[k].count
=0;
                    
int kk=0;
                    
//更新其它cache块没使用时间
                    while(kk<m)
                    
{
                        
if(kk!=k)
                        cache[kk].count
++;
                        kk
++;
                    }

                    
break;
                }

                k
++;
            }

            
            
if(k==m)//cache满且没有命中的情况,考虑置换那一块.
            {
                
int ii=0;
                
int t=0;//要替换的cache块号.
                int max=cache[ii].count;
                ii
++;
                
while(ii<m)  //LRU
                {
                    
if(cache[ii].count>max)
                    
{
                        max
=cache[ii].count;
                        t
=ii;
                    }

                    ii
++;
                }

                
//置换
                cout<<cache[t].value<<""<<walk_sort[i]<<"在cache的"<<t<<"号块置换..."<<endl;
                cache[t].value
=walk_sort[i++];
                cache[t].count
=0;
                
for (x=0;x<m;x++)
                cout
<<"cache块"<<x<<""<<cache[x].value<<endl;
                
int kk=0;
                
//更新其它cache块没使用时间
                while(kk<m)
                    
{
                        
if(kk!=t)
                        cache[kk].count
++;
                        kk
++;
                    }

                delay();
            }

        }

    }

}


void  delay()
{
    
int i;
    
for(i=0;i<100;i++)
    ;
}



void  main( int  argc,  char *  argv[])
{
    cout
<<"Cache更新LRU置换算法"<<endl;
    up_cache(cache,walk_sort);
}
 
LRU算法是一种缓存算法,它的全称是Least Recently Used,即最近最少使用算法。该算法的思想是,当缓存满时,淘汰最近最少使用的数据。这种算法可以有效地提高缓存的命中率,从而提高系统性能。 下面是一个简单的LRU页面调度算法模拟程序,代码如下: ```python class LRUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.lru_list = [] def get(self, key): if key in self.cache: # 更新访问记录 self.lru_list.remove(key) self.lru_list.append(key) return self.cache[key] else: return -1 def put(self, key, value): if key in self.cache: # 更新访问记录 self.lru_list.remove(key) self.lru_list.append(key) self.cache[key] = value else: # 淘汰最近最少使用的数据 if len(self.cache) >= self.capacity: del self.cache[self.lru_list[0]] del self.lru_list[0] self.cache[key] = value self.lru_list.append(key) # 测试程序 cache = LRUCache(3) cache.put(1, 1) cache.put(2, 2) cache.put(3, 3) print(cache.get(1)) # 返回 1 cache.put(4, 4) print(cache.get(2)) # 返回 -1 cache.put(5, 5) print(cache.get(3)) # 返回 -1 print(cache.get(4)) # 返回 4 print(cache.get(5)) # 返回 5 ``` 在这个程序中,LRUCache类表示一个缓存对象,它包含三个成员变量:capacity表示缓存的容量,cache是一个字典,用于存储缓存数据,lru_list是一个列表,用于记录最近访问的数据。 代码中的get()方法用于查询缓存中是否存在指定的键,如果存在,则更新访问记录,并返回对应的值;否则返回-1。put()方法用于向缓存中添加数据,如果数据已经存在,则更新访问记录;否则,先检查缓存是否已满,如果已满,则淘汰最近最少使用的数据,并将新数据添加到缓存中。 在测试程序中,我们创建了一个容量为3的缓存对象,向其中添加了三个数据,然后查询了第一个数据,再添加两个数据,最后查询了三个数据。运行结果符合预期,证明了LRU页面调度算法的正确性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值