This commit is contained in:
GuanM 2024-11-15 09:06:20 +08:00
commit 758636f091

View File

@ -1,41 +1,48 @@
class 进程: class 进程:
def __init__(self, 名称, 到达时间, 执行时间): def __init__(self, 名称, 到达时间, 执行时间, 优先级):
self.名称 = 名称 self.名称 = 名称
self.到达时间 = 到达时间 self.到达时间 = 到达时间
self.执行时间 = 执行时间 self.执行时间 = 执行时间
self.剩余时间 = 执行时间 self.剩余时间 = 执行时间
self.优先级 = 优先级
self.完成时间 = 0
self.顺序 = 0 self.顺序 = 0
def 最短剩余时间调度():
def 优先级调度():
进程列表 = [] 进程列表 = []
for i in range(4): for i in range(4):
名称, 到达时间, 执行时间 = input().split() 名称, 到达时间, 执行时间, 优先级 = input().split()
进程列表.append(进程(名称, int(到达时间), int(执行时间))) 进程列表.append(进程(名称, int(到达时间), int(执行时间), int(优先级)))
进程列表[-1].顺序 = i 进程列表[-1].顺序 = i
当前时间 = 0 当前时间 = 0
完成的进程 = [] 完成的进程 = []
结果 = []
while len(完成的进程) < 4: while len(完成的进程) < 4:
可用进程 = [p for p in 进程列表 if p.到达时间 <= 当前时间 and p not in 完成的进程] 可用进程 = [p for p in 进程列表 if p.到达时间 <= 当前时间 and p not in 完成的进程]
if not 可用进程: if not 可用进程:
下一个到达 = min([p.到达时间 for p in 进程列表 if p not in 完成的进程]) 下一个到达 = min([p.到达时间 for p in 进程列表 if p not in 完成的进程])
当前时间 = 下一个到达 当前时间 = 下一个到达
continue continue
选择的进程 = min(可用进程, key=lambda x: (x.剩余时间, x.顺序)) 选择的进程 = max(可用进程, key=lambda x: (x.优先级, -x.顺序))
if not 结果 or 结果[-1] != 选择的进程.名称: for p in 可用进程:
结果.append(选择的进程.名称) if p != 选择的进程:
p.优先级 += 1
选择的进程.优先级 -= 1
选择的进程.剩余时间 -= 1 选择的进程.剩余时间 -= 1
当前时间 += 1 当前时间 += 1
if 选择的进程.剩余时间 == 0: if 选择的进程.剩余时间 == 0:
选择的进程.完成时间 = 当前时间
完成的进程.append(选择的进程) 完成的进程.append(选择的进程)
结果 = [str(p.完成时间) for p in sorted(进程列表, key=lambda x: x.顺序)]
print(" ".join(结果)) print(" ".join(结果))
最短剩余时间调度()
优先级调度()