Last active
April 10, 2023 03:58
-
-
Save aasmpro/7703a5055c1dae72fe02157f9c06b3c3 to your computer and use it in GitHub Desktop.
Python implementation of Banker's algorithm, written in Python 3
This file contains hidden or 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
def main(): | |
processes = int(input("number of processes : ")) | |
resources = int(input("number of resources : ")) | |
max_resources = [int(i) for i in input("maximum resources : ").split()] | |
print("\n-- allocated resources for each process --") | |
currently_allocated = [[int(i) for i in input(f"process {j + 1} : ").split()] for j in range(processes)] | |
print("\n-- maximum resources for each process --") | |
max_need = [[int(i) for i in input(f"process {j + 1} : ").split()] for j in range(processes)] | |
allocated = [0] * resources | |
for i in range(processes): | |
for j in range(resources): | |
allocated[j] += currently_allocated[i][j] | |
print(f"\ntotal allocated resources : {allocated}") | |
available = [max_resources[i] - allocated[i] for i in range(resources)] | |
print(f"total available resources : {available}\n") | |
running = [True] * processes | |
count = processes | |
while count != 0: | |
safe = False | |
for i in range(processes): | |
if running[i]: | |
executing = True | |
for j in range(resources): | |
if max_need[i][j] - currently_allocated[i][j] > available[j]: | |
executing = False | |
break | |
if executing: | |
print(f"process {i + 1} is executing") | |
running[i] = False | |
count -= 1 | |
safe = True | |
for j in range(resources): | |
available[j] += currently_allocated[i][j] | |
break | |
if not safe: | |
print("the processes are in an unsafe state.") | |
break | |
print(f"the process is in a safe state.\navailable resources : {available}\n") | |
if __name__ == '__main__': | |
main() |
Good day.I tried running your code on my PC but it’s giving me an indentation error in line 15 do you know how to fix it?
thanks , helped a lot <3
Good day.I tried running your code on my PC but it’s giving me an indentation error in line 15 do you know how to fix it?
hi! what was the error?
thanks , helped a lot <3
ty!
No worries. I already resolved it
Can I have the code segment for printing the safe sequence?
@BILWANATH-2002 Hey! actually, i don't have time to do it, it's all here, just change it as you need. ;)
nice code but too complex to understand.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example outputs
number of processes : 5
number of resources : 4
maximum resources : 8 5 9 7
-- allocated resources for each process --
process 1 : 2 0 1 1
process 2 : 0 1 2 1
process 3 : 4 0 0 3
process 4 : 0 2 1 0
process 5 : 1 0 3 0
-- maximum resources for each process --
process 1 : 3 2 1 4
process 2 : 0 2 5 2
process 3 : 5 1 0 5
process 4 : 1 5 3 0
process 5 : 3 0 3 3
total allocated resources : [7, 3, 7, 5]
total available resources : [1, 2, 2, 2]
process 3 is executing
the process is in a safe state.
available resources : [5, 2, 2, 5]
process 1 is executing
the process is in a safe state.
available resources : [7, 2, 3, 6]
process 2 is executing
the process is in a safe state.
available resources : [7, 3, 5, 7]
process 4 is executing
the process is in a safe state.
available resources : [7, 5, 6, 7]
process 5 is executing
the process is in a safe state.
available resources : [8, 5, 9, 7]
number of processes : 5
number of resources : 3
maximum resources : 3 3 2
-- allocated resources for each process --
process 1 : 0 1 0
process 2 : 1 0 0
process 3 : 1 0 1
process 4 : 0 1 1
process 5 : 1 1 0
-- maximum resources for each process --
process 1 : 7 5 3
process 2 : 3 2 3
process 3 : 4 2 5
process 4 : 7 3 2
process 5 : 2 3 2
total allocated resources : [3, 3, 2]
total available resources : [0, 0, 0]
the processes are in an unsafe state.