From 6da50ed90f6229b61cff4bd67aa6b07dc6d5e341 Mon Sep 17 00:00:00 2001 From: GuanM <30427262+sxhoio@users.noreply.github.com.> Date: Thu, 14 Nov 2024 17:26:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 操作系统/题目3.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/操作系统/题目3.py b/操作系统/题目3.py index 6387ec0..76ef435 100644 --- a/操作系统/题目3.py +++ b/操作系统/题目3.py @@ -1,41 +1,48 @@ class 进程: - def __init__(self, 名称, 到达时间, 执行时间): + def __init__(self, 名称, 到达时间, 执行时间, 优先级): self.名称 = 名称 self.到达时间 = 到达时间 self.执行时间 = 执行时间 self.剩余时间 = 执行时间 + self.优先级 = 优先级 + self.完成时间 = 0 self.顺序 = 0 -def 最短剩余时间调度(): + +def 优先级调度(): 进程列表 = [] for i in range(4): - 名称, 到达时间, 执行时间 = input().split() - 进程列表.append(进程(名称, int(到达时间), int(执行时间))) + 名称, 到达时间, 执行时间, 优先级 = input().split() + 进程列表.append(进程(名称, int(到达时间), int(执行时间), int(优先级))) 进程列表[-1].顺序 = i 当前时间 = 0 完成的进程 = [] - 结果 = [] while len(完成的进程) < 4: 可用进程 = [p for p in 进程列表 if p.到达时间 <= 当前时间 and p not in 完成的进程] - + if not 可用进程: 下一个到达 = min([p.到达时间 for p in 进程列表 if p not in 完成的进程]) 当前时间 = 下一个到达 continue - 选择的进程 = min(可用进程, key=lambda x: (x.剩余时间, x.顺序)) - - if not 结果 or 结果[-1] != 选择的进程.名称: - 结果.append(选择的进程.名称) + 选择的进程 = max(可用进程, key=lambda x: (x.优先级, -x.顺序)) + + for p in 可用进程: + if p != 选择的进程: + p.优先级 += 1 + 选择的进程.优先级 -= 1 选择的进程.剩余时间 -= 1 当前时间 += 1 if 选择的进程.剩余时间 == 0: + 选择的进程.完成时间 = 当前时间 完成的进程.append(选择的进程) + 结果 = [str(p.完成时间) for p in sorted(进程列表, key=lambda x: x.顺序)] print(" ".join(结果)) -最短剩余时间调度() \ No newline at end of file + +优先级调度() \ No newline at end of file