 ### Python Implementation of Bankers Algorithm

Python Implementation of Banker's Algorithm
Description:
Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used in banking systems to determine whether a loan can be granted or not.Whenever a new process is created, it must exactly
specify the maximum instances of each resource type that it needs. The banker's algorithm is a method used in deadlock avoidance technique in multiple instances of a resource type.
The various data structures used in it are:
Let us assume that there are n processes and m resource types. Some data structures are used to implement the banker's algorithm. They are:
Available:
It is an array of length m. It represents the number of available resources of each type. If Available[j] = k, then there are k instances available, of resource type Rj.
Max: It is an n x m matrix which represents the maximum number of instances of each resource that a
process can request. If Max[i][j] = k, then the process Pi can request utmost k instances of resource type Rj.
Allocation: It is an n x m matrix which represents the number of resources of each type currently allocated to each process. If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.
Need:
It is an n x m matrix which indicates the remaining resource needs of each process. If Need[i][j] = k, then process Pi may need k more instances of resource type Rj to complete its task.
Need[i][j] = Max[i][j] - Allocation [i][j]

Program :

``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 =  * 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()``
Output:

``number of processes : 5number of resources : 4maximum resources : 8 5 9 7-- allocated resources for each process --process 1 : 2 0 1 1process 2 : 0 1 2 1process 3 : 4 0 0 3process 4 : 0 2 1 0process 5 : 1 0 3 0-- maximum resources for each process --process 1 : 3 2 1 4process 2 : 0 2 5 2process 3 : 5 1 0 5process 4 : 1 5 3 0process 5 : 3 0 3 3total allocated resources : [7, 3, 7, 5]total available resources : [1, 2, 2, 2]process 3 is executingthe process is in a safe state.available resources : [5, 2, 2, 5]process 1 is executingthe process is in a safe state.available resources : [7, 2, 3, 6]process 2 is executingthe process is in a safe state.available resources : [7, 3, 5, 7]process 4 is executingthe process is in a safe state.available resources : [7, 5, 6, 7]process 5 is executingthe process is in a safe state.available resources : [8, 5, 9, 7]`` 