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

这篇博客详细介绍了LRU(最近最少使用)和FIFO(先进先出)两种页面替换算法的原理,并通过Python代码进行了实现。作者首先解释了两种算法的工作方式,然后展示了如何在内存满时进行页面置换。在代码部分,分别定义了两个类`ALU`,用于模拟这两种算法,最后通过实例运行展示了不同阶段的缺页次数,从而对比了两种算法的性能。

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

箫毒赵药师

嘿嘿嘿

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

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

打赏作者

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

抵扣说明:

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

余额充值