other.bankers_algorithm

The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes a “s-state” check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.

[Source] Wikipedia
[Credit] Rosetta Code C implementation helped very much.

Attributes

test_allocated_res_table

test_claim_vector

test_maximum_claim_table

Classes

BankersAlgorithm

Module Contents

class other.bankers_algorithm.BankersAlgorithm(claim_vector: list[int], allocated_resources_table: list[list[int]], maximum_claim_table: list[list[int]])
__available_resources() list[int]

Check for available resources in line with each resource in the claim vector

__need() list[list[int]]

Implement safety checker that calculates the needs by ensuring that max_claim[i][j] - alloc_table[i][j] <= avail[j]

__need_index_manager() dict[int, list[int]]

This function builds an index control dictionary to track original ids/indices of processes when altered during execution of method “main”

Return:

{0: [a: int, b: int], 1: [c: int, d: int]}

>>> index_control = BankersAlgorithm(
...     test_claim_vector, test_allocated_res_table, test_maximum_claim_table
... )._BankersAlgorithm__need_index_manager()
>>> {key: [int(x) for x in value] for key, value
...     in index_control.items()}  
{0: [1, 2, 0, 3], 1: [0, 1, 3, 1], 2: [1, 1, 0, 2], 3: [1, 3, 2, 0],
 4: [2, 0, 0, 3]}
__pretty_data()

Properly align display of the algorithm’s solution

__processes_resource_summation() list[int]

Check for allocated resources in line with each resource in the claim vector

main(**kwargs) None
Utilize various methods in this class to simulate the Banker’s algorithm
Return:

None

>>> BankersAlgorithm(test_claim_vector, test_allocated_res_table,
...    test_maximum_claim_table).main(describe=True)
         Allocated Resource Table
P1       2        0        1        1

P2       0        1        2        1

P3       4        0        0        3

P4       0        2        1        0

P5       1        0        3        0

         System Resource Table
P1       3        2        1        4

P2       0        2        5        2

P3       5        1        0        5

P4       1        5        3        0

P5       3        0        3        3

Current Usage by Active Processes: 8 5 9 7
Initial Available Resources:       1 2 2 2
__________________________________________________

Process 3 is executing.
Updated available resource stack for processes: 5 2 2 5
The process is in a safe state.

Process 1 is executing.
Updated available resource stack for processes: 7 2 3 6
The process is in a safe state.

Process 2 is executing.
Updated available resource stack for processes: 7 3 5 7
The process is in a safe state.

Process 4 is executing.
Updated available resource stack for processes: 7 5 6 7
The process is in a safe state.

Process 5 is executing.
Updated available resource stack for processes: 8 5 9 7
The process is in a safe state.
__allocated_resources_table
__claim_vector
__maximum_claim_table
other.bankers_algorithm.test_allocated_res_table = [[2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0]]
other.bankers_algorithm.test_claim_vector = [8, 5, 9, 7]
other.bankers_algorithm.test_maximum_claim_table = [[3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3]]