操作系统模拟之请求分页算法。
文件共1份,代码如下:

import math
import os
import random
import copydef alo_opt():print("您选择了OPT算法,执行结果如下:")print("访问页面  物理块  缺页中断")temp_queue = []sum_changed = 0for i in range(page_size):flag_changed = Falseif len(temp_queue) < temp_queue_size and temp_queue.count(page_queue[i]) == 0:temp_queue.append(page_queue[i])elif temp_queue.count(page_queue[i]) == 0:appear_position = []for j in range(temp_queue_size):appear_position.append(page_size)for j in range(i + 1, page_size):if temp_queue.count(page_queue[j]) > 0 and appear_position[temp_queue.index(page_queue[j])] == page_size:appear_position[temp_queue.index(page_queue[j])] = jremotest_position = appear_position.index(max(appear_position))temp_queue[remotest_position] = page_queue[i]sum_changed = sum_changed + 1flag_changed = Trueoutput_queue = copy.deepcopy(temp_queue)for j in range(temp_queue_size - len(temp_queue)):output_queue.append("&")print('{}  {}  {}'.format(page_queue[i], output_queue, flag_changed))print()print('页面访问序列总长{0},OPT算法共发生缺页中断{1}次,缺页率为{1}/{0}'.format(page_size, sum_changed))def alo_fifo():print("您选择的是先进先出页面置换算法")print("访问页面  物理块  缺页中断")temp_queue = []sum_changed = 0appear_time = []for i in range(page_size+1):appear_time.append(page_size)for i in range(page_size):flag_changed = Falseif len(temp_queue) < temp_queue_size and temp_queue.count(page_queue[i]) == 0:temp_queue.append(page_queue[i])appear_time[page_queue[i]] = ielif temp_queue.count(page_queue[i]) == 0:target_index = 0min_time = page_sizefor j in range(0, temp_queue_size):if appear_time[temp_queue[j]] < min_time:min_time = appear_time[temp_queue[j]]target_index = jappear_time[temp_queue[target_index]] = page_sizetemp_queue[target_index] = page_queue[i]appear_time[page_queue[i]] = isum_changed = sum_changed + 1flag_changed = Trueoutput_queue = copy.deepcopy(temp_queue)for j in range(temp_queue_size - len(temp_queue)):output_queue.append("&")print('{}  {}  {}'.format(page_queue[i], output_queue, flag_changed))print()print('页面访问序列总长{0},FIFO算法共发生缺页中断{1}次,缺页率为{1}/{0}'.format(page_size, sum_changed))def alo_lru():print("您选择的是最近最久未使用页面置换算法")print("访问页面  物理块  缺页中断")temp_queue = []sum_changed = 0recent_used = []for i in range(page_size+1):recent_used.append(0)for i in range(page_size):flag_changed = Falseif len(temp_queue) < temp_queue_size and temp_queue.count(page_queue[i]) == 0:temp_queue.append(page_queue[i])elif temp_queue.count(page_queue[i]) == 0:target_index = 0min_time = page_sizefor j in range(0, temp_queue_size):if recent_used[temp_queue[j]] < min_time:min_time = recent_used[temp_queue[j]]target_index = jtemp_queue[target_index] = page_queue[i]recent_used[page_queue[i]] = isum_changed = sum_changed + 1flag_changed = Trueelse:recent_used[page_queue[i]] = ioutput_queue = copy.deepcopy(temp_queue)for j in range(temp_queue_size - len(temp_queue)):output_queue.append("&")print('{}  {}  {}'.format(page_queue[i], output_queue, flag_changed))print()print('最近最久未使用页面置换算法缺页{0}次, 缺页率为{0}/{1}'.format(sum_changed, page_size))def alo_clock():print("您选择的是时钟页面置换算法")print("访问页面  物理块  缺页中断")temp_queue = []sum_changed = 0flag = {}for i in range(0, page_size+1):flag[i] = Falsepoint = 0for i in range(page_size):flag_changed = Falseif len(temp_queue) < temp_queue_size and temp_queue.count(page_queue[i]) == 0:temp_queue.append(page_queue[i])flag[page_queue[i]] = Truepoint = temp_queue.index(page_queue[i]) + 1elif temp_queue.count(page_queue[i]) == 1:point = temp_queue.index(page_queue[i])flag[page_queue[i]] = Trueelif temp_queue.count(page_queue[i]) == 0:while True:if not flag[temp_queue[point % temp_queue_size]]:temp_queue[point % temp_queue_size] = page_queue[i]point = point + 1sum_changed = sum_changed + 1flag_changed = Truebreakelse:flag[temp_queue[point % temp_queue_size]] = Falsepoint = point + 1output_queue = copy.deepcopy(temp_queue)for j in range(len(temp_queue)):if flag[temp_queue[j]]:output_queue[j] = str(temp_queue[j]) + "*"for j in range(temp_queue_size - len(temp_queue)):output_queue.append("&")print('{}  {}  {}'.format(page_queue[i], output_queue, flag_changed))print()print('时钟页面置换算法缺页{0}次, 缺页率为{0}/{1}'.format(sum_changed, page_size))print("欢迎进入操作系统课程演示系统之页面置换算法!")
print()flag_exit = False
while True:if flag_exit == True:print("您已成功退出系统!")breakprint("请输入页面访问序列长度(15-25,含端点):")while True:page_size = int(input())if page_size >= 15 and page_size <= 25:breakprint("您输入的页面访问序列长度超出给定范围,请重新输入15-25(含端点)的数字:")print("请输入物理块个数(3-5,含端点):")while True:temp_queue_size = int(input())if temp_queue_size >= 3 and temp_queue_size <= 5:breakprint("您输入的物理块个数超出给定范围,请重新输入3-5(含端点)的数字:")print()page_queue = []print("选择用户输入请输入1")print("选择随机生成请输入2")data_produce_choice = int(input())if data_produce_choice == 2:for i in range(page_size):page_queue.append(random.randint(0, 5))else:while True:print('请输入0-5数字组成的无规律字符串作为页面访问序列,长度为{}'.format(page_size))flag_digit = Truetemp = input().split(" ")key = ["0", "1", "2", "3", "4", "5"]for i in temp:if key.count(i) == 0:flag_digit = Falseif not flag_digit:print("您输入的序列有误,请重新输入")elif len(temp) < page_size:print('您已输入{}长度的字符串,还需输入{}长度的字符串'.format(len(temp), page_size - len(temp)))print('请继续输入0-5数字组成的无规律字符串作为页面访问序列,长度为{}'.format(page_size - len(temp)))temp_plus = input().split(" ")if len(temp_plus) != page_size - len(temp):print("您再次输入错误,序列需要全部重新输入")continuefor i in temp:page_queue.append(int(i))for i in temp_plus:page_queue.append(int(i))elif len(temp) > page_size:print("您输入的字符串超长,系统将进行自动截取Y或由用户重新输入N(Y/N)")if input() == "Y":for i in range(page_size):page_queue.append(int(temp[i]))breakelse:continueelse:for i in temp:page_queue.append(int(i))breakprint()print('页面访问序列为:{}'.format(page_queue))print('物理块个数为:{}'.format(temp_queue_size))print()while True:print("选择最佳置换算法请输入1")print("选择先进先出页面置换算法请输入2")print("选择最近最久未使用页面置换算法请输入3")print("选择时钟页面置换算法请输入4")menu_choice = int(input())if menu_choice == 1:alo_opt()elif menu_choice == 2:alo_fifo()elif menu_choice == 3:alo_lru()elif menu_choice == 4:alo_clock()else:print("您的算法选择错误,请重新输入!")print()print("继续尝试其他算法请输入1")print("更换数据请输入2")print("退出程序请输入3")end_choice = int(input())if end_choice == 2:breakelif end_choice == 3:flag_exit = True

请求分页算法 Python实现相关推荐

  1. 棋盘最短路径 python_Dijkstra 最短路径算法 Python 实现

    Dijkstra 最短路径算法 Python 实现 问题描述 使用 Dijkstra 算法求图中的任意顶点到其它顶点的最短路径(求出需要经过那些点以及最短距离). 以下图为例: 算法思想 可以使用二维 ...

  2. 2021-03-15 数据挖掘算法—K-Means算法 Python版本

    数据挖掘算法-K-Means算法 Python版本 简介 又叫K-均值算法,是非监督学习中的聚类算法. 基本思想 k-means算法比较简单.在k-means算法中,用cluster来表示簇:容易证明 ...

  3. 2021-01-28 粒子群优化算法-Python版本和Matlab函数 particleswarm 调用

    粒子群优化算法-Python版本和Matlab函数 particleswarm 调用 前两天分享了粒子群优化算法的原理和Matlab原理实现,本文分享一下Python代码下的PSO实现以及Matlab ...

  4. 最优化算法python实现篇(4)——无约束多维极值(梯度下降法)

    最优化算法python实现篇(4)--无约束多维极值(梯度下降法) 摘要 算法简介 注意事项 算法适用性 python实现 实例运行结果 算法过程可视化 摘要 本文介绍了多维无约束极值优化算法中的梯度 ...

  5. 最优化算法python实现篇(3)——无约束一维极值(黄金分割法)

    最优化算法python实现篇(3)--无约束一维极值(黄金分割法) 算法适用问题 python实现 示例运行结果 算法适用问题 搜索给定单峰区间的极值问题,一般对凸优化问题比较适用. python实现 ...

  6. 最优化算法python实现篇(2)—无约束一维极值(二分法)

    最优化算法python实现篇(2)--无约束一维极值(二分法) 算法适用问题 python实现 示例运行结果 算法适用问题 搜索给定单峰区间的极值问题,一般对凸优化问题比较适用. python实现 # ...

  7. 多元线性回归算法python实现_手写算法-Python代码推广多元线性回归

    1.梯度下降-矩阵形式 上篇文章介绍了一元线性回归,包括Python实现和sklearn实现的实例.对比,以及一些问题点,详情可以看这里: 链接: 手写算法-Python代码实现一元线性回归 里面封装 ...

  8. 哈希运算python实现_一致性哈希算法 python实现

    # -*- coding: utf-8 -*- """ 一致性哈希算法 python实现 参考 http://weblogs.java.net/blog/2007/11/ ...

  9. (四)协同过滤算法之基于用户的推荐算法python实现

    一.背景 关于推荐算法的相关背景介绍,已经在上一个姊妹篇(三)协同过滤算法之基于物品的推荐算法python实现中有所介绍.在此,便不在赘述,本文主要介绍基于用户的协同过滤算法,而对推荐算法不太清楚的朋 ...

最新文章

  1. redis日志_「漫画」谈谈Redis持久化
  2. linux系统不关机添加硬盘吗,CentOS中不重启添加硬盘
  3. 为了新零售,A.O.史密斯做出一个令人吃惊的决定
  4. 四针手表指的是什么_1000~1500元的男士手表推荐
  5. python变量/分支/循环/数组/列表/元组/序列
  6. 一个能极大提高生产率的Chrome新建标签页扩展
  7. ES6 解构赋值详解
  8. 流式计算之Storm简介
  9. 阶乘很简单?恕我直言,阶乘相关的面试题你还真不一定懂!
  10. 隐藏在浏览器背后的“黑手”
  11. 开发APP软件需要多少钱?
  12. android offtime编程,OFFTIME - 做功课就憋玩手机了! - Android 应用 - 【最美应用】
  13. 惠普HP LaserJet Pro P1106 打印机驱动
  14. Thinkpad 笔记本电源状态指示灯
  15. linux 的vim快速查找内容
  16. CRM对接企业微信日程快速实现提醒功能
  17. 武田公布2019财年第一季度强劲业绩并上调全年指引
  18. php mysql echarts动态生成图表
  19. 为什么工控行业生意越来越难做了?
  20. 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇一:WPF常用知识以及本项目设计总结...

热门文章

  1. 链表之删除双链表倒数第K个节点
  2. linux用户取消密码,[Linux]linux下取消用户名和密码直接登录
  3. 软件项目组织管理(九)项目人力资源管理
  4. 女生来大姨妈该怎么哄她?
  5. 岛国小姐姐来例假时,男朋友背着她偷偷查手机......
  6. 把 14 亿中国人民都拉到一个微信群里在技术上能实现吗?
  7. 你们都被电视剧版的 《西游记》给骗了!| 今日趣图
  8. 当才华还撑不起梦想时,你应该静下心来看这些
  9. 数据庞大繁杂,如何精简挖掘?
  10. 超级智能玩具《小小机器人》首发|全新50种玩法,创造力之源