-
-
Save zaxboy24/ba17924783c9eff886b3a84972a82e25 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment