0% found this document useful (0 votes)
119 views2 pages

Banker's Algorithm for CS Students

The document describes the Banker's Algorithm for determining a safe sequence of processes that can be executed without resulting in a deadlock. It defines allocated resources, maximum resources, and future needs for 4 processes. It then applies the Banker's Algorithm to determine if a safe sequence exists by incrementally allocating available resources. If a safe sequence is found for all processes, it is printed.

Uploaded by

Brijesh Kuvadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views2 pages

Banker's Algorithm for CS Students

The document describes the Banker's Algorithm for determining a safe sequence of processes that can be executed without resulting in a deadlock. It defines allocated resources, maximum resources, and future needs for 4 processes. It then applies the Banker's Algorithm to determine if a safe sequence exists by incrementally allocating available resources. If a safe sequence is found for all processes, it is printed.

Uploaded by

Brijesh Kuvadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like