Bankers_Algorithm 17/04/20, 6:26 PM
Banker's Algorithm
In [7]: import pandas as pd
import numpy as np
In [8]: Allocated = {
'p1':[1, 0, 2, 1, 1],
'p2':[2, 0, 1, 1, 0],
'p3':[1, 1, 0, 1, 1],
'p4':[1, 1, 1, 1, 0]
}
Max = {
'p1':[1, 1, 2, 1, 3],
'p2':[2, 2, 2, 1, 0],
'p3':[2, 1, 3, 1, 1],
'p4':[1, 1, 2, 2, 0]
}
Allocated = pd.DataFrame(Allocated).transpose()
Max = pd.DataFrame(Max).transpose()
futureNeed = Max - Allocated
aval = [0,0,2,1,1]
print(" FUTURE NEED ")
print(futureNeed.to_string(header = False) )
FUTURE NEED
p1 0 1 0 0 2
p2 0 2 1 0 0
p3 1 0 3 0 0
p4 0 0 1 1 0
http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-practicals/Bankers_Algorithm.ipynb?download=false Page 1 of 2
Bankers_Algorithm 17/04/20, 6:26 PM
In [9]: SafeSeq = []
flag = len(futureNeed)
while(flag>0):
for x in range(len(futureNeed)):
label = futureNeed.index.values[x]
req = list(futureNeed.iloc[x])
freed = list(Allocated.iloc[x])
if(aval >= req and label not in SafeSeq):
SafeSeq.append(label)
aval = list(np.array(aval) + np.array(freed))
flag = flag-1
if len(SafeSeq)<len(Allocated):
print("\033[31mNO SAFE-SEQ FOUND!!\033[0m")
print(SafeSeq)
print(len(Allocated))
print(len(SafeSeq))
print
else:
print("Safe Sequence = ",SafeSeq)
Safe Sequence = ['p4', 'p1', 'p2', 'p3']
http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-practicals/Bankers_Algorithm.ipynb?download=false Page 2 of 2