Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Forked from rafidalwahid/Bankers_Algorithm.py
Created November 13, 2022 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anan5a/fed733f62a507aa30ebb0e5f53a9ce6b to your computer and use it in GitHub Desktop.
Save Anan5a/fed733f62a507aa30ebb0e5f53a9ce6b to your computer and use it in GitHub Desktop.
# P0, P1, P2, P3, P4 are the Process names here
n = 5 # int(input('Enter number of process: ')) # Number of processes
m = 3 # int(input('Enter number of resources: ')) # Number of resources
# Allocation Matrix
alloc = [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]] #
# for i in range(n):
# alloc.append(list(map(int, input(f'Enter {i + 1} allocation: ').split(' '))))
# MAX Matrix
max = [[7, 5, 3], [3, 2, 2], [9, 0, 2], [2, 2, 2], [4, 3, 3]] #
# for i in range(n):
# max.append(list(map(int, input(f'Enter {i + 1} max need: ').split(' '))))
avail = [3, 3, 2] # list(map(int, input(f'Enter available resources: ').split(' '))) # # Available Resources
f = [0] * n # state
ans = [0] * n
ind = 0 # index of ans
need = [[0] * m for i in range(n)]
for i in range(n):
for j in range(m):
need[i][j] = max[i][j] - alloc[i][j]
for k in range(n):
for i in range(n):
if f[i] == 0:
flag = 0
for j in range(m):
if need[i][j] > avail[j]:
flag = 1
break
if flag == 0:
ans[ind] = i
ind += 1
for y in range(m):
avail[y] += alloc[i][y]
f[i] = 1
print("Following is the SAFE Sequence")
for i in range(n - 1):
print(" P", ans[i], " ->", sep="", end="")
print(" P", ans[n - 1], sep="")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment