0% found this document useful (0 votes)
9 views

Bankers Algorithm

Uploaded by

Dixxy Scott
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Bankers Algorithm

Uploaded by

Dixxy Scott
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Bankers Algorithm

NumberOfProcesses = int(input("Enter the number of processes: "))


NoOfResources = int(input("Enter the number of resources: "))

allocMat = []
maxMat = []
avail = []

for i in range(NumberOfProcesses):
rowAllocMax = []
rowMaxMat = []
rowAvailMat = []
for j in range(NoOfResources):
rowAllocMax.append(int(input(f"Enter element {i},{j} for allocation matrix: ")))

rowMaxMat.append(int(input(f"Enter element {i},{j} for max mat: ")))


if i == 0:
rowAvailMat.append(int(input(f"Enter element {j} for available matrix: ")))
else:
continue

allocMat.append(rowAllocMax)
maxMat.append(rowMaxMat)
avail.append(rowAvailMat)

f = [0] * NumberOfProcesses
ans = [0] * NumberOfProcesses
ind = 0
for k in range(NumberOfProcesses):
f[k] = 0

need = [[0 for i in range(NumberOfResources)] for i in range(NumberOfProcesses)]


for i in range(NumberOfProcesses):
for j in range(NumberOfResources):
need[i][j] = maxMat[i][j] - allocMat[i][j]

y=0

for k in range(5):
for i in range(NumberOfProcesses):
if f[i] == 0:
flag = 0
for j in range(NumberOfResources):
if need[i][j] > avail[j]:
flag = 1
break

if flag == 0:
ans[ind] = i
ind += 1
for y in range(NumberOfResources):
avail[y] += allocMat[i][y]
f[i] = 1

print("SAFE Sequence: ")


for i in range(NumberOfProcesses - 1):
print(" P", ans[i], " ->", sep="", end="")
print(" P", ans[NumberOfProcesses - 1], sep="")

Output:

You might also like