other.bankers_algorithm ======================= .. py:module:: other.bankers_algorithm .. autoapi-nested-parse:: 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. | (https://rosettacode.org/wiki/Banker%27s_algorithm) Attributes ---------- .. autoapisummary:: other.bankers_algorithm.test_allocated_res_table other.bankers_algorithm.test_claim_vector other.bankers_algorithm.test_maximum_claim_table Classes ------- .. autoapisummary:: other.bankers_algorithm.BankersAlgorithm Module Contents --------------- .. py:class:: BankersAlgorithm(claim_vector: list[int], allocated_resources_table: list[list[int]], maximum_claim_table: list[list[int]]) .. py:method:: __available_resources() -> list[int] Check for available resources in line with each resource in the claim vector .. py:method:: __need() -> list[list[int]] Implement safety checker that calculates the needs by ensuring that ``max_claim[i][j] - alloc_table[i][j] <= avail[j]`` .. py:method:: __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()} # doctest: +NORMALIZE_WHITESPACE {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]} .. py:method:: __pretty_data() Properly align display of the algorithm's solution .. py:method:: __processes_resource_summation() -> list[int] Check for allocated resources in line with each resource in the claim vector .. py:method:: 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. .. py:attribute:: __allocated_resources_table .. py:attribute:: __claim_vector .. py:attribute:: __maximum_claim_table .. py:data:: test_allocated_res_table :value: [[2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0]] .. py:data:: test_claim_vector :value: [8, 5, 9, 7] .. py:data:: test_maximum_claim_table :value: [[3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3]]