-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bankers Algorithm.py
107 lines (92 loc) · 3.86 KB
/
Bankers Algorithm.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Amount_Process = int(input("How many Process: "))
Amount_Resources = int(input("How many Resources: "))
Amount_Process_Iterator=1
Allocation_Amount=1
Allocation_iterator=1
Processes=[]
Max_Need=[]
Available_Work=[[]]
Need=[]
Order=[]
for x in range(0,Amount_Process):
Need.append([])
Processes.append([])
Max_Need.append([])
for x in range(0,Amount_Resources):
Amount_AvailWork = int(input("Available work for Resource {}: ".format(x+1)))
Available_Work[0].append(Amount_AvailWork)
for x in range(0,Amount_Process):
print("Allocation for P{}".format(Amount_Process_Iterator))
for y in range(0,Amount_Resources):
Allocate=int(input("Enter Amount of element {} Allocated: ".format(Allocation_iterator)))
Processes[Amount_Process_Iterator-1].append(Allocate) #p1,p2,p3
Allocation_iterator+=1 #Resource 1,2,3
Allocation_iterator = 1
Amount_Process_Iterator+=1
Amount_Process_Iterator=1
for x in range(0,Amount_Process):
print("Max Needed for P{}".format(Amount_Process_Iterator))
for y in range(0,Amount_Resources):
Allocate=int(input("Enter Amount of Max Needed for element {} : ".format(Allocation_iterator)))
Max_Need[Amount_Process_Iterator-1].append(Allocate) #p1,p2,p3
Allocation_iterator+=1 #Resource 1,2,3
Allocation_iterator = 1
Amount_Process_Iterator+=1
def Bankers_Algorithm(Processes,Max_Need,Available_Work):
#generate need
Need_Iterator = 0
Element_Iterator = 0
Resultant_Available_Work=[]
Processes_Copy=Processes
Success_Count = 1
Tuple_Need = tuple(Need)
Available_Work_Iterator=0
for x in range(0,Amount_Process): #X element of Y list
for y in range(0,Amount_Resources): #depending on how many resources
Max_Need_Value=Max_Need[Need_Iterator][Element_Iterator] #Max_Need[0][0]
Process_Value=Processes[Need_Iterator][Element_Iterator] #Process_Value[0][0]
Result=Max_Need_Value-Process_Value
Need[Need_Iterator].append(Result)
Element_Iterator += 1
Element_Iterator = 0
Need_Iterator += 1
Need_Iterator=0
while len(Need) != 0:
Need_Value=Need[Need_Iterator][Element_Iterator]
Available_Work_Value=Available_Work[Available_Work_Iterator][Element_Iterator]
if Available_Work_Value >= Need_Value:
Element_Iterator += 1 #Check Next Need Value and compare to Next Avialable Work Value
Success_Count += 1 #if success count =3 then append meaning can allocate
if Success_Count == Amount_Resources:
for y in range(0,Amount_Resources):
Add = Available_Work[-1][y] + Processes_Copy[Need_Iterator][y]
Resultant_Available_Work.append(Add)
Available_Work.append(Resultant_Available_Work)
Index=Tuple_Need.index(Need[Need_Iterator])
Order.append(Index)
Processes_Copy.pop(Need_Iterator)
Need.pop(Need_Iterator)
Resultant_Available_Work=[]
Success_Count=1
Element_Iterator=0
Available_Work_Iterator += 1
if Need_Iterator >= len(Need):
Need_Iterator = 0 # go back to the start
else:
Need_Iterator+=1 #move on to next process
print("Available Work")
for x in range(0, len(Available_Work)):
print(Available_Work[x])
print("Need")
for x in range(0, len(Tuple_Need)):
print("P{}:{} ".format(x+1,Tuple_Need[x]))
print("Order")
for x in range(0,len(Order)):
print("P{}".format(Order[x]+1))
print("Allocation")
for x in range(0,len(Processes)):
print("P{}:{} ".format(x+1,Processes[x]))
print("Max_Need")
for x in range(0,len(Max_Need)):
print("P{}:{} ".format(x+1,Max_Need[x]))
Bankers_Algorithm(Processes,Max_Need,Available_Work)