最近做实验,要求用文件存取数据,上网搜,主要都是C语言或者Java写的,Python的粗略看是没搜到。
所以想到用Python来写, *** 作文件更方便一些。
废话不多说,直接上代码
class load_from_file: file_name = None #文件名 file = None #文件对象 is_pre = None #是否有预先页 block = None #物理块数量 pre_list = None #预先页内容 visit_list = None #访问列表 def __init__(self) -> None: self.load("page_src.txt") def load(self,name): self.file_name = name self.file = open(self.file_name,"r+") fr = self.file.read() str1 = fr.split("n") list1 = str1[0].split(" ")#文件中保存两行数据 list2 = str1[1].split(" ") for i in range(len(list1)): list1[i] = eval(list1[i]) for i in range(len(list2)): list2[i] = eval(list2[i]) self.block = list1[0]#第一行第一个数字表示有几个物理块 if list1[1] != 0:#第一行第二个数据表示是否有预先页,后面的数据则是预先页中的内容,后面的数据项长度应该小于第一个数字 self.is_pre = True self.pre_list = list1[2:] if len(self.pre_list) > self.block: print("错误,请检查传入文件内容是否正确") return self.visit_list = list2#第二行表示访问页的列表 self.file.close() class dispatch: is_pre = None block = None pre_list = None now_list = None #当前访问页列表 visit_list = None is_lack = None #是否缺页 res_list = None #保存结果便于写入文件 def __init__(self) -> None:#初始化数据 self.block = load_from_file().block self.is_pre = load_from_file().is_pre self.pre_list = load_from_file().pre_list self.visit_list = load_from_file().visit_list self.now_list = [0]*self.block self.is_lack = [True]*len(self.visit_list) self.res_list = [] def OPT(self) -> list: if self.is_pre: for i in range(len(self.pre_list)): self.now_list[i] = self.pre_list[i] future_count = [-1]*self.block for i in range(len(self.visit_list)): if self.visit_list[i] in self.now_list:#如果访问的页在当前表中,则不缺页 self.is_lack[i] = False #print(self.now_list) res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) continue if 0 in self.now_list: self.now_list[self.now_list.index(0)] = self.visit_list[i] res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) continue for j in range(self.block):#缺页,计算最久访问页 if self.now_list[j] in self.visit_list[i:]: future_count[j] = self.visit_list[i:].index(self.now_list[j]) else: future_count[j] = len(self.visit_list) self.now_list[future_count.index(max(future_count))] = self.visit_list[i]#替换 #print(self.now_list) res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) return self.res_list def FIFO(self): if self.is_pre: for i in range(len(self.pre_list)): self.now_list[i] = self.pre_list[i] exist_count = [0]*self.block self.pre_list = [x for x in self.now_list] for i in range(len(self.visit_list)): for j in range(self.block): if self.now_list[j] == self.pre_list[j]: exist_count[j] += 1 else: exist_count[j] = 0 if self.visit_list[i] in self.now_list:#如果访问的页在当前表中,则不缺页 self.is_lack[i] = False res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) #print(self.now_list) continue if 0 in self.now_list: self.now_list[self.now_list.index(0)] = self.visit_list[i] res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) continue self.pre_list = [i for i in self.now_list]#储存未置换前的页 self.now_list[exist_count.index(max(exist_count))] = self.visit_list[i] #print(self.now_list) res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) return self.res_list def LRU(self): if self.is_pre: for i in range(len(self.pre_list)): self.now_list[i] = self.pre_list[i] unused_time = [0]*self.block for i in range(len(self.visit_list)): if self.visit_list[i] in self.now_list:#如果访问的页在当前表中,则不缺页 self.is_lack[i] = False res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) #print(self.now_list) continue if 0 in self.now_list: self.now_list[self.now_list.index(0)] = self.visit_list[i] res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) continue for j in range(self.block): if self.now_list[j] not in self.visit_list[:i]: unused_time[j] = len(self.visit_list) else: temp_list = [x for x in self.visit_list[:i]] temp_list.reverse() unused_time[j] = temp_list.index(self.now_list[j]) self.now_list[unused_time.index(max(unused_time))] = self.visit_list[i] #print(self.now_list) res_temp = [x for x in self.now_list] res_temp.append(self.is_lack[i]) self.res_list.append(res_temp) return self.res_list def main(self): while True: print("1-OPTt2-FIFOt3-LRU") choose = input("请选择调度方式:") res = [] method = "" if choose == "1": res = dispatch().OPT() method = "OPT" elif choose == "2": res = dispatch().FIFO() method = "FIFO" elif choose == "3": res = dispatch().LRU() method = "LRU" else: print("程序退出") return print(method,end="") for i in self.visit_list: print("t"+str(i),end="") print() count_lack = 0 for i in range(len(res[0])): if i == len(res[0]) - 1: print("缺页",end="") for j in range(len(self.visit_list)): if res[j][i] == True: count_lack += 1 else: print("块"+str(i),end="") for j in range(len(self.visit_list)): print("t"+str(res[j][i]),end="") print() print("缺页率",str(count_lack/len(self.is_lack)*100)+"%") with open("page_res.txt","w",encoding="utf-8") as ff: ff.write(method+" ") for i in self.visit_list: ff.write("t"+str(i)) ff.write("n") for i in range(len(res[0])): if i == len(res[0]) - 1: ff.write("缺页") else: ff.write("块"+str(i)+" ") for j in range(len(self.visit_list)): if i == len(res[0]) - 1: if res[j][i] == True: ff.write("t1") else: ff.write("t0") else: ff.write("t"+str(res[j][i])) ff.write("n") ff.write("1代表缺页,0代表不缺页n缺页率 "+str(count_lack/len(self.is_lack)*100)+"%") ff.close() if __name__ == "__main__": dispatch().main()
实验要求实现OPT、FIFO和LRU算法,非常简单
下面是测试用的源文件page_src.txt的内容
3 1 1 2 3 1 2 4 2 6 2 1 5 6 1
测试结果
OPT
OPT 1 2 4 2 6 2 1 5 6 1 块0 1 1 1 1 1 1 1 1 1 1 块1 2 2 2 2 2 2 2 5 5 5 块2 3 3 4 4 6 6 6 6 6 6 缺页 0 0 1 0 1 0 0 1 0 0 1代表缺页,0代表不缺页 缺页率 30.0%
FIFO
FIFO 1 2 4 2 6 2 1 5 6 1 块0 1 1 4 4 4 4 1 1 1 1 块1 2 2 2 2 6 6 6 5 5 5 块2 3 3 3 3 3 2 2 2 6 6 缺页 0 0 1 0 1 1 1 1 1 0 1代表缺页,0代表不缺页 缺页率 60.0%
LRU
LRU 1 2 4 2 6 2 1 5 6 1 块0 1 1 1 1 6 6 6 5 5 5 块1 2 2 2 2 2 2 2 2 6 6 块2 3 3 4 4 4 4 1 1 1 1 缺页 0 0 1 0 1 0 1 1 1 0 1代表缺页,0代表不缺页 缺页率 50.0%
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)