From f95f1446bfcc81f746138e29640940ae56acc74f Mon Sep 17 00:00:00 2001 From: GuanM <30427262+sxhoio@users.noreply.github.com.> Date: Thu, 7 Nov 2024 17:24:47 +0800 Subject: [PATCH] =?UTF-8?q?E=E9=A2=98=E7=9B=AE=E7=AD=94=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 操作系统/题目5.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 操作系统/题目5.py diff --git a/操作系统/题目5.py b/操作系统/题目5.py new file mode 100644 index 0000000..e884852 --- /dev/null +++ b/操作系统/题目5.py @@ -0,0 +1,53 @@ +def 安全检查(可用, 最大需求, 分配, 进程): + n = len(进程) + 工作 = 可用.copy() + 完成 = [False] * n + 安全序列 = [] + + 需求 = [] + for i in range(n): + 需求.append([最大需求[i][j] - 分配[i][j] for j in range(3)]) + + while True: + 找到 = False + for i in range(n): + if not 完成[i] and all(需求[i][j] <= 工作[j] for j in range(3)): + for j in range(3): + 工作[j] += 分配[i][j] + 完成[i] = True + 安全序列.append(进程[i]) + 找到 = True + break + + if not 找到: + break + + return all(完成), 安全序列 + +def 主函数(): + 资源 = list(map(int, input().split())) + + 进程 = [] + 最大需求 = [] + 分配 = [] + + for _ in range(4): + 行 = input().split() + 进程.append(行[0]) + 最大需求.append(list(map(int, 行[1:4]))) + 分配.append(list(map(int, 行[4:7]))) + + 可用 = 资源.copy() + for i in range(4): + for j in range(3): + 可用[j] -= 分配[i][j] + + 安全状态, 序列 = 安全检查(可用, 最大需求, 分配, 进程) + + if 安全状态: + print(" ".join(序列)) + else: + print("false") + +if __name__ == "__main__": + 主函数() \ No newline at end of file