python 实现LRU置换算法和先进先出置换算法

LRU就是在内存中寻找,需要的页面,没有找到,就插入内存,内存满了,就把内存中最长时间没有使用的页面置换。

先进先出置换算法就是在内存中找,没有找到,就插入内存,内存满了,最先进入内存的页面,出内存,将需要的页面插入末尾。

图解请去找百度。

代码:

LRU:

import numpy as np
import random

class ALU:
    App_cishu=0#申请次数
    absent_page=0#缺页
    def __init__(self,memory_page,time,page):
        self.memory_page=memory_page#内存
        self.page=page#页面
        self.time=time#每个页面停留时间
        

    def inter_dui(self):#第一次入队,内存未满
        i=0#页面下标
        
        while(True):
            self.App_cishu=self.App_cishu+1#申请次数
            app_num=random.randint(1,31)#属于前5000次,申请1-30
            #print("申请使用%d号页面"%app_num)
            if app_num in self.memory_page:
                #在内存中找到了该页号,该时间置零,其他时间必须加一,因为如果碰巧每一次都找到了,最终所有停留时间相同 且内存满了的情况。                 
                index=np.where(self.memory_page==app_num)
                self.time[index]=0 #访问过程中,在内存中找到了,停留时间重置为零
                for c in range (i):#为了与后续的满内存置换,此处使用先置零再加一
                    self.time[c]=self.time[c]+1 
               #print("%d页面已在内存"%app_num)
            else:#没找到,外存调入,每个存在页面的时间加+1
               #print("%d页面不存在内存,将执行置换调入"%app_num)
                self.memory_page[i]=app_num
                i=i+1
                self.absent_page=self.absent_page+1
                for c in range (i):#入队的页面停留时间+1
                    self.time[c]=self.time[c]+1         
                if (i==30):#内存满,退出
                    break

    def Appreciate(self,cishu,x,y):#入队完成后,开始调用此函数开始置换,x,y申请的页面号范围
        for i in range(cishu):
            self.App_cishu=self.App_cishu+1
            app_num=random.randint(x,y)#申请
            #print("申请使用%d号页面"%app_num)
            if app_num in self.memory_page:  #找到

                #print("%d页面已在内存"%app_num)
                index=np.where(self.memory_page==app_num)
                self.time[index]=0# 停留时间重置为零    
                for c in range (30):
                    self.time[c]=self.time[c]+1 
            else:#没找到,开始置换
                self.absent_page=self.absent_page+1#缺页+1
                #print("%d页面不存在内存,将执行置换"%app_num)
                index=np.where(self.time==max(self.time))
                self.memory_page[index]=app_num
                self.time[index]=0#还是先置零再加一
                for c in range (30):
                    self.time[c]=self.time[c]+1 

    def show_memory(self):#内存展示       
        print(self.memory_page,"\n",self.time)
    def show_queye(self):
        print("申请次数",self.App_cishu)
        print("缺页:",self.absent_page)
        print("缺页率:",self.absent_page/self.App_cishu)
    
    def diaoyong(self):
        ALU.inter_dui(self)
        ALU.Appreciate(self,5000-self.App_cishu,1,30)
        #ALU.show_queye(self)
        ALU.Appreciate(self,40000,30,51)
        print("前45000次,缺页",self.absent_page)
        ALU.Appreciate(self,5000,51,101)
        print("前50000次,缺页",self.absent_page)

memory_page=np.array(np.zeros(30))#内存
time=np.zeros(30)#停留时间
page=[i for i in range (1,101)]#页面号

a=ALU(memory_page,time,page)
a.diaoyong()
a.show_queye()

先进先出置换算法:

import numpy as np
import random

class ALU:
    App_cishu=0#申请次数
    absent_page=0#缺页
    def __init__(self,memory_page):
        self.memory_page=memory_page#内存
               
    def inter_dui(self):#第一次入队,内存未满
        i=0#页面下标        
        while(True):
            self.App_cishu=self.App_cishu+1#申请次数
            app_num=random.randint(1,31)#属于前5000次,申请1-30
            #print("申请使用%d号页面"%app_num)
            if app_num in self.memory_page:
                #在内存中找到了该页号,跳过               
                continue
                #print("%d页面已在内存"%app_num)
            else:#没找到,外存调入,每个存在页面的时间加+1
               #print("%d页面不存在内存,将执行置换调入"%app_num)
                self.memory_page[i]=app_num
                i=i+1
                self.absent_page=self.absent_page+1      
                if (i==len(self.memory_page)):#内存满,退出
                    break
            #print("满内存之前的缺页",self.absent_page)

    def Appreciate(self,cishu,x,y):#入队完成后,开始调用此函数开始置换,x,y申请的页面号范围
        for i in range(cishu):
            self.App_cishu=self.App_cishu+1
            app_num=random.randint(x,y)#申请
            #print("申请使用%d号页面"%app_num)
            if app_num in self.memory_page:  #找到
                #print("%d页面已在内存"%app_num)
                continue
            else:#没找到,开始置换
                self.absent_page=self.absent_page+1#缺页+1
                #print("%d页面不存在内存,将执行置换"%app_num)
                for c in range(len(self.memory_page)-1):#队列前移,在末尾调入内存
                    self.memory_page[c]=self.memory_page[c+1]
                self.memory_page[len(self.memory_page)-1]=app_num#末尾调入

    def show_memory(self):#内存展示       
        print(self.memory_page,"\n",self.time)
    def show_queye(self):
        print("总申请次数",self.App_cishu)
        print("缺页:",self.absent_page)
        print("缺页率:",self.absent_page/self.App_cishu)
    def diaoyong(self):
        ALU.inter_dui(self)
        
        ALU.Appreciate(self,5000-self.App_cishu,1,30)
        print("前5000次,缺页",self.absent_page)
        ALU.Appreciate(self,40000,30,51)
        print("前45000次,缺页",self.absent_page)
        ALU.Appreciate(self,5000,51,101)
        print("前50000次,缺页",self.absent_page)
memory_page=np.array(np.zeros(30))#内存
a=ALU(memory_page)
a.diaoyong()
a.show_queye()

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LRU页面置换算法实现需要以下步骤: 1. 建立一个双向链表,用于维护页面的使用顺序。链表头表示最近最少使用的页面,链表尾表示最近使用的页面。 2. 建立一个哈希表,用于快速查找页面是否在内存中,并保存页面在链表中的位置。 3. 当需要访问一个页面时,先在哈希表中查找是否在内存中。如果在内存中,将该页面移动到链表头;如果不在内存中,进行页面置换。 4. 当内存不足时,需要选择一个页面进行置换。选择链表尾的页面进行置换,因为它是最近最少使用的页面。 5. 从链表尾删除该页面,并从哈希表中删除该页面的记录。 6. 将新页面加入链表头,并在哈希表中记录该页面的位置。 7. 重复上述步骤,直到访问完所有页面。 下面是一个简单的Python实现: ```python class LRUCache: def __init__(self, capacity: int): self.capacity = capacity self.cache = {} self.head = Node(0, 0) self.tail = Node(0, 0) self.head.next = self.tail self.tail.prev = self.head def get(self, key: int) -> int: if key in self.cache: node = self.cache[key] self._remove(node) self._add(node) return node.val else: return -1 def put(self, key: int, value: int) -> None: if key in self.cache: self._remove(self.cache[key]) node = Node(key, value) self.cache[key] = node self._add(node) if len(self.cache) > self.capacity: node = self.head.next self._remove(node) del self.cache[node.key] def _add(self, node): p = self.tail.prev p.next = node node.prev = p node.next = self.tail self.tail.prev = node def _remove(self, node): p = node.prev n = node.next p.next = n n.prev = p class Node: def __init__(self, k, v): self.key = k self.val = v self.prev = None self.next = None ``` 其中,LRUCache类实现LRU页面置换算法,get方法用于获取页面,put方法用于添加页面,_add和_remove方法用于链表操作,Node类表示链表节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵药师

嘿嘿嘿

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值