52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
class 进程:
|
|
def __init__(self, 名称, 到达时间, 执行时间):
|
|
self.名称 = 名称
|
|
self.到达时间 = 到达时间
|
|
self.执行时间 = 执行时间
|
|
self.剩余时间 = 执行时间
|
|
|
|
|
|
def 最短作业优先调度():
|
|
进程列表 = []
|
|
for _ in range(4):
|
|
名称, 到达时间, 执行时间 = input().split()
|
|
进程列表.append(进程(名称, int(到达时间), int(执行时间)))
|
|
|
|
当前时间 = 0
|
|
完成数 = 0
|
|
当前进程 = None
|
|
执行序列 = []
|
|
|
|
while 完成数 < 4:
|
|
可用进程 = [p for p in 进程列表
|
|
if p.到达时间 <= 当前时间 and p.剩余时间 > 0]
|
|
|
|
if not 可用进程:
|
|
下一个到达 = min([p.到达时间 for p in 进程列表
|
|
if p.剩余时间 > 0])
|
|
当前时间 = 下一个到达
|
|
continue
|
|
|
|
最短作业 = min(可用进程,
|
|
key=lambda x: (x.剩余时间, 进程列表.index(x)))
|
|
|
|
if 当前进程 is None or 当前进程 != 最短作业:
|
|
当前进程 = 最短作业
|
|
执行序列.append(最短作业.名称)
|
|
|
|
当前时间 += 1
|
|
最短作业.剩余时间 -= 1
|
|
|
|
if 最短作业.剩余时间 == 0:
|
|
完成数 += 1
|
|
当前进程 = None
|
|
|
|
结果 = []
|
|
for i in range(len(执行序列)):
|
|
if i == 0 or 执行序列[i] != 执行序列[i - 1]:
|
|
结果.append(执行序列[i])
|
|
|
|
print(" ".join(结果))
|
|
|
|
|
|
最短作业优先调度() |