-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChandyMisraHaas (Deadlock).py
59 lines (40 loc) · 1.18 KB
/
ChandyMisraHaas (Deadlock).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
print('''
P1 P2 P3 P4 P5
P1 0 1 0 0 0
P2 0 0 1 0 0
P3 0 0 0 1 1
P4 1 0 0 0 0
P5 0 0 0 0 0
''')
a = [ [0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0] ]
flag = 0
def aman(a, i, k):
end = 5
for x in range(end):
if(a[k][x] == 1):
if(i == x):
print(f' S{k+1} ==> S{x+1} ({i+1}, {k+1}, {x+1}) --------> DEADLOCK DETECTED')
global flag
flag = 1
break
print(f' S{k+1} ==> S{x+1} ({i+1}, {k+1}, {x+1})')
aman(a,i,x)
print("CHANDY-MISRA-HAAS DISTRIBUTED DEADLOCK DETECTION ALGORITHM")
print("__________________________________________________________")
print()
x = 0
end = 5
i = int(input("Enter Initiator Site No. : "))
j = i - 1
print()
for k in range(end):
if(a[j][k]==1):
print(f' S{j+1} ==> s{k+1} ({i}, {j+1}, {k+1})')
aman(a,j,k)
if(flag == 0):
print("\nNO DEADLOCK DETECTED")
print("__________________________________________________________")