Question: Operating system Topic: dead lock (Banker's Algorithm) Question: Explain details whole code of each line .(I am giving my code I will give thumbs up

Operating system

Topic: dead lock (Banker's Algorithm)

Question: Explain details whole code of each line .(I am giving my code I will give thumbs up for nice answer)

CODE:

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(" -- allocated resources for each process --") currently_allocated = [[int(i) for i in input(f"process {j + 1} : ").split()] for j in range(processes)]

print(" -- 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" total allocated resources : {allocated}")

available = [max_resources[i] - allocated[i] for i in range(resources)] print(f"total available resources : {available} ")

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. available resources : {available} ")

if __name__ == '__main__': main()

Picture for avoid line indentation error:

Operating system Topic: dead lock (Banker's Algorithm) Question: Explain details whole code

def main(): processes = int(input("number of processes : ")) int(input("number of resources : ")) max_resources = [int(i) for i in input("maximum resources :").split()] resources = print(" -- allocated resources for each process --") currently_allocated = [[int(i) for i in input(f"process {j + 1} : ").split()] for j in range (processes)] print(" -- 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(" total allocated resources : allocated}") available = [max_resources[i] - allocated[i] for i in range(resources)] print("total available resources : {available} ") 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("the process is in a safe state. Inavailable resources : {available} ") if name _main__': main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!