-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankers-Algorithm.py
More file actions
40 lines (34 loc) · 1.16 KB
/
Bankers-Algorithm.py
File metadata and controls
40 lines (34 loc) · 1.16 KB
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
allocated = [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
max_need = [[7, 5, 3], [3, 2, 2], [9, 0, 2], [4, 2, 2], [5, 3, 3]]
total_resources = [10, 5, 7]
num_processes = 5
safe_Seq = []
need = [
[max_need[i][j] - allocated[i][j] for j in range(len(max_need[0]))]
for i in range(num_processes)
]
print("Need matrix:")
print(need)
allocated_sum = [
sum(allocated[i][j] for i in range(num_processes))
for j in range(len(total_resources))
]
available = [total_resources[j] - allocated_sum[j] for j in range(len(total_resources))]
print("Initial Available Resources:", available)
finish = [False] * num_processes
work = available.copy()
print(work)
while True:
found = False
for i in range(num_processes):
if not finish[i] and all(need[i][j] <= work[j] for j in range(len(available))):
work = [work[j] + allocated[i][j] for j in range(len(available))]
print(work)
finish[i] = True
safe_Seq.append(i)
found = True
break
if not found:
break
print("All processes finished: ", all(finish))
print("Safe sequence: ", safe_Seq if all(finish) else "No safe seq found")