Last active
November 13, 2022 03:46
-
-
Save rafidalwahid/5541d0f266a72d3e36a89fdba25a4bd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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