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