如何编写模型检测程序?
流程、资源、请求和发布列表:
A req R
A req S
A rel R
A rel S
B req S
B req R
B rel S
B rel R
无法重新安排流程中的顺序(在上面的示例中,A 必须始终在请求 S 之前先请求 R),但是可以重新安排跨流程的顺序(B 可以在 A 请求 R 之前请求 S 和 R)
每个排序都将输出带有标题、排序以及订单是否导致死锁的指示。
输出我想要的结果:-
ORDER 1: A req R, A req S, A rel R, A rel S, B req S, B req R, B rel S, B rel R
ORDER 2: A req R, A req S, A rel R, B req S, A rel S, B req R, B rel S, B rel R
...
ORDER X: A req R, B req S, A req S, B req R DEADLOCK!
测试用例输入:-
案例一:-
A req R A req S A rel R A rel S B req S B req T B rel S B rel T C req T
C req R C rel T C rel R
案例2:-
A req R A req S A rel R A rel S B req T B rel T C req S C rel S D req U
D req S D req T D rel U D rel S D rel T E req T E req V E rel T E rel V
F req W F req S F rel W F rel S G req V G req U G rel V G rel U
from itertools import permutations
class Resource:
def __init__(self, name):
self.name = name
self.holder = None
self.waiting_list = []
def request(self, process):
if self.holder is None:
self.holder = process
return True
else:
self.waiting_list.append(process)
return False
def release(self):
if len(self.waiting_list) == 0:
self.holder = None
else:
next_process = self.waiting_list.pop(0)
self.holder = next_process
return next_process
class Process:
def __init__(self, name):
self.name = name
self.resources = []
def add_resource(self, resource):
self.resources.append(resource)
def request_resources(self):
for resource in self.resources:
if not resource.request(self):
return False
return True
def release_resources(self):
for resource in self.resources:
next_process = resource.release()
if next_process is not None:
next_process.request_resources()
def simulate_ordering(processes):
for ordering in permutations(processes):
for process in ordering:
if not process.request_resources():
break
else:
for process in reversed(ordering):
process.release_resources()
return True
return False
def detect_deadlock(processes):
if simulate_ordering(processes):
print("No deadlock detected.")
else:
print("Deadlock detected!")
if __name__ == "__main__":
# File 1 input
r = Resource("R")
s = Resource("S")
t = Resource("T")
processes = [
Process("A"),
Process("B"),
Process("C")
]
for process in processes:
process.add_resource(r)
process.add_resource(s)
process.add_resource(t)
processes[0].add_resource(t)
processes[0].add_resource(r)
processes[1].add_resource(s)
processes[1].add_resource(r)
processes[2].add_resource(t)
processes[2].add_resource(r)
detect_deadlock(processes)
# File 2 input
r = Resource("R")
s = Resource("S")
t = Resource("T")
u = Resource("U")
v = Resource("V")
w = Resource("W")
processes = [
Process("A"),
Process("B"),
Process("C"),
Process("D"),
Process("E"),
Process("F"),
Process("G")
]
for process in processes:
process.add_resource(r)
process.add_resource(s)
process.add_resource(t)
process.add_resource(u)
process.add_resource(v)
process.add_resource(w)
processes[0].add_resource(r)
processes[0].add_resource(s)
processes[1].add_resource(t)
processes[2].add_resource(s)
processes[3].add_resource(u)
processes[3].add_resource(s)
processes[3].add_resource(t)
processes[4].add_resource(t)
processes[4].add_resource(v)
processes[5].add_resource(w)
processes[5].add_resource(s)
processes[6].add_resource(v)
processes[6].add_resource(u)
detect_deadlock(processes)
上面的程序打印输出如下
Deadlock detected!
现在,我怎样才能像上面提到的那样打印结果。请指导我打印输出。谢谢。