请求分页算法 Python实现

操作系统模拟之请求分页算法。
文件共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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/312717.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在 Visual Studio 2019 中连接中国版 Azure

点击上方蓝字关注“汪宇杰博客”导语做国内项目很可能用到中国版的 Azure&#xff0c;即世纪互联运营的 azure.cn。然而 Visual Studio 2019 默认连接的是国际版的 Azure&#xff0c;如何添加中国版订阅呢&#xff1f;差点9966年前我曾经写过一篇《图解&#xff1a;如何在Visua…

磁盘寻道算法 Python实现

操作系统模拟之磁盘寻道算法。 文件共1份&#xff0c;代码如下&#xff1a; import math import random import copydef alo_fcfs():print("您选择了FCFS算法&#xff0c;执行结果如下&#xff1a;")print("当前磁道号 下一磁道号 绝对差")print({:6d}{:1…

k8s 使用 Init Container 确保依赖的服务已经启动

k8s 使用 Init Container 确保依赖的服务已经启动Intro最近 helm 3 正式发布了&#xff0c;dotnetcore 3.1 也正式发布了&#xff0c;最近打算把我的活动室预约项目做一个升级&#xff0c;项目已经升级到了 dotnetcore 3.1&#xff0c;最近几天则在准备把项目打包一个 helm 包&…

LeetCode动态规划 最大子序和

给定一个整数数组 nums &#xff0c;找到一个具有最大和的连续子数组&#xff08;子数组最少包含一个元素&#xff09;&#xff0c;返回其最大和。 ps&#xff1a; 清华大学912曾经考过。 状态转移方程 dp[i] max(nums[i], dp[i-1] nums[i]) 边界条件 dp[0] nums[0] 代码…

dotNET面试题汇总系列连载(1):基础语法

点击上方“dotNET全栈开发”&#xff0c;“设为星标”加“星标★”&#xff0c;每天11.50&#xff0c;好文必达全文约4000字&#xff0c;预计阅读时间8分钟马上要到2020年了&#xff0c;这里整理一个.NET 工程师面试题系列&#xff0c;希望年底或者明年金三银四跳槽的程序猿们带…

LeetCode动态规划 环形子数组的最大和

给定一个由整数数组 A 表示的环形数组 C&#xff0c;求 C 的非空子数组的最大可能和。 此外&#xff0c;子数组最多只能包含固定缓冲区 A 中的每个元素一次。 思路 本道题需要分类成两种情况&#xff0c;题目答案为以下两种情况的较大值。 1.最大子区间在序列的中间 这种情况是…

Blazor 版 Bootstrap Admin 通用后台权限管理框架

前言上一篇介绍过了前后台分离的 NET Core 通用权限管理系统 在这篇文章简要的介绍了 Bootstrap Admin 后台管理框架的一些功能。本篇文章带来的是微软最新出的 Blazor 版本的 NET Core 通用权限管理系统Blazor 简介至于 Blazor 是什么&#xff0c;Blazor 的优缺点小伙伴们可以…

xms跨平台基础框架 - 基于.netcore

背景鄙人经过多年开发&#xff0c;数百个项目“打磨(折磨)”&#xff0c;各种国内外框架平台都有涉及&#xff0c;没有一款称心顺手的&#xff0c;原因有三&#xff0c;一是设计反人类&#xff0c;二是不开源根本无法突破框架限制&#xff0c;三是即使开源也是阉割版&#xff0…

如何让 Azure AD 里的应用只允许特定用户登录

点击上方蓝字关注“汪宇杰博客”导语我的博客系统支持 Azure Active Directory 身份认证&#xff0c;然而用VS点点鼠标配出来的 Azure 应用&#xff0c;默认情况下会允许所有 AAD 内的用户访问&#xff0c;如何限制为只让特定用户访问呢&#xff1f;背景情况其实最近&#xff0…

程序员:这10种糟糕的程序命名,你遇到过几个?

点击上方“dotNET全栈开发”&#xff0c;“设为星标”加“星标★”&#xff0c;每天11.50&#xff0c;好文必达全文约2300字&#xff0c;预计阅读时间4分钟有人问&#xff1a;规范的命名风格真的能让你程序员少出bug&#xff1f;当遇到这方面的教训时&#xff0c;就会想到这句话…

评测OJ时间复杂度

平日里做OJ题时&#xff0c;先看数据范围能帮助自己选择算法 &#xff08;可能更多时候是看暴力能骗到几分qwq) 一般而言&#xff0c;评测OJ在1s内能接受的算法时间复杂度是10e8-10e9之间&#xff0c;这里取折中值5*10e8。 对于不同的算法&#xff0c;能够接受的最大数据如下&…

.Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换

1.什么是API网关API网关是微服务架构中的唯一入口&#xff0c;它提供一个单独且统一的API入口用于访问内部一个或多个API。它可以具有身份验证&#xff0c;监控&#xff0c;负载均衡&#xff0c;缓存&#xff0c;请求分片与管理&#xff0c;静态响应处理等。API网关方式的核心要…

[Asp.net core 3.1] 通过一个小组件熟悉Blazor服务端组件开发

通过一个小组件&#xff0c;熟悉 Blazor 服务端组件开发。github:https://github.com/git-net/NBlazors一、环境搭建vs2019 16.4, asp.net core 3.1 新建 Blazor 应用&#xff0c;选择 asp.net core 3.1。根文件夹下新增目录 Components&#xff0c;放置代码。二、组件需求定义…

【STM32】STM32学习笔记-TIM定时中断(13)

00. 目录 文章目录 00. 目录01. TIM简介02. 定时器类型03. 基本定时器04. 通用定时器05. 高级定时器06. 定时中断基本结构07. 预分频器时序08. 计数器时序09. 计数器无预装时序10. 计数器有预装时序11. RCC时钟树12. 附录 01. TIM简介 TIM&#xff08;Timer&#xff09;定时器…

开源.Net Standard版华为物联网北向接口SDK

说明最近用到了华为的物联网平台API&#xff0c;但是官方没有.Net版的SDK&#xff0c;所以就自己封装了一个&#xff0c;开源出来给有需要的朋友&#xff0c;同时也算是为.Net Core的发展做点小贡献~源码地址&#xff1a;https://github.com/iamoldli/HuaWei.IoT.NorthApi.Sdk同…

你知道怎么使用DebugView查看调试信息吗?

简介 DebugView是sysinternals工具集中的一款用来查看调试信息的工具。不管你是内核开发人员还是应用程序开发人员&#xff0c;都会用到这款神器。先简单看看DebugView可以干什么吧。可以查看应用程序输出的调试信息。可以查看驱动程序输出的调试信息。可以查看本地机器的调试信…

使用ASP.NET Core 3.x 构建 RESTful API - 3.3.3 ProblemDetails

当ASP.NET Core 大约在 2.1 版本的时候&#xff0c;它引入了 ProblemDetails。ProblemDetails是基于 RFC7807 这个规范&#xff0c;目的是让 HTTP 响应可以携带错误的详细信息&#xff0c;而不是只返回一个错误的状态码。 在 ASP.NET Core 2.2的时候&#xff0c;如果Controller…

(译)An introduction to Kubernetes

原文&#xff1a;https://www.jeremyjordan.me/kubernetes/&#xff08;博客园团队推荐的&#xff09;这篇博客文章将对Kubernetes进行介绍&#xff0c;以便您了解该工具背后的动机&#xff0c;含义以及使用方式。在后续文章中&#xff0c;我将讨论如何使用更具体的&#xff08…

WeihanLi.Npoi 支持 ShadowProperty 了

WeihanLi.Npoi 支持 ShadowProperty 了Intro在 EF 里有个 ShadowProperty (阴影属性/影子属性)的概念&#xff0c;你可以通过 FluentAPI 的方式来定义一个不在 .NET model 里定义的属性&#xff0c;只能通过 EF 里的 ChangeTracker 来操作这种属性。在导出 Excel 的时候&#x…

RNN循环神经网络概述

RNN与普通神经网络的区别&#xff1a;能够更好的处理序列的信息 RNN结构图 如图所示&#xff0c;St的值不仅取决于输入X的值&#xff0c;还取决于St-1的值。同时&#xff0c;St-1的值还取决于St-2的值&#xff0c;因此S1,S2,…,St-1的值都与St的值直接或间接相关&#xff0c;…