TheAlgorithms-Python
222 строки · 8.2 Кб
1# A Python implementation of the Banker's Algorithm in Operating Systems using
2# Processes and Resources
3# {
4# "Author: "Biney Kingsley (bluedistro@github.io), bineykingsley36@gmail.com",
5# "Date": 28-10-2018
6# }
7"""
8The Banker's algorithm is a resource allocation and deadlock avoidance algorithm
9developed by Edsger Dijkstra that tests for safety by simulating the allocation of
10predetermined maximum possible amounts of all resources, and then makes a "s-state"
11check to test for possible deadlock conditions for all other pending activities,
12before deciding whether allocation should be allowed to continue.
13[Source] Wikipedia
14[Credit] Rosetta Code C implementation helped very much.
15(https://rosettacode.org/wiki/Banker%27s_algorithm)
16"""
17
18from __future__ import annotations19
20import numpy as np21
22test_claim_vector = [8, 5, 9, 7]23test_allocated_res_table = [24[2, 0, 1, 1],25[0, 1, 2, 1],26[4, 0, 0, 3],27[0, 2, 1, 0],28[1, 0, 3, 0],29]
30test_maximum_claim_table = [31[3, 2, 1, 4],32[0, 2, 5, 2],33[5, 1, 0, 5],34[1, 5, 3, 0],35[3, 0, 3, 3],36]
37
38
39class BankersAlgorithm:40def __init__(41self,42claim_vector: list[int],43allocated_resources_table: list[list[int]],44maximum_claim_table: list[list[int]],45) -> None:46"""47:param claim_vector: A nxn/nxm list depicting the amount of each resources
48(eg. memory, interface, semaphores, etc.) available.
49:param allocated_resources_table: A nxn/nxm list depicting the amount of each
50resource each process is currently holding
51:param maximum_claim_table: A nxn/nxm list depicting how much of each resource
52the system currently has available
53"""
54self.__claim_vector = claim_vector55self.__allocated_resources_table = allocated_resources_table56self.__maximum_claim_table = maximum_claim_table57
58def __processes_resource_summation(self) -> list[int]:59"""60Check for allocated resources in line with each resource in the claim vector
61"""
62return [63sum(p_item[i] for p_item in self.__allocated_resources_table)64for i in range(len(self.__allocated_resources_table[0]))65]66
67def __available_resources(self) -> list[int]:68"""69Check for available resources in line with each resource in the claim vector
70"""
71return np.array(self.__claim_vector) - np.array(72self.__processes_resource_summation()73)74
75def __need(self) -> list[list[int]]:76"""77Implement safety checker that calculates the needs by ensuring that
78max_claim[i][j] - alloc_table[i][j] <= avail[j]
79"""
80return [81list(np.array(self.__maximum_claim_table[i]) - np.array(allocated_resource))82for i, allocated_resource in enumerate(self.__allocated_resources_table)83]84
85def __need_index_manager(self) -> dict[int, list[int]]:86"""87This function builds an index control dictionary to track original ids/indices
88of processes when altered during execution of method "main"
89Return: {0: [a: int, b: int], 1: [c: int, d: int]}
90>>> (BankersAlgorithm(test_claim_vector, test_allocated_res_table,
91... test_maximum_claim_table)._BankersAlgorithm__need_index_manager()
92... ) # doctest: +NORMALIZE_WHITESPACE
93{0: [1, 2, 0, 3], 1: [0, 1, 3, 1], 2: [1, 1, 0, 2], 3: [1, 3, 2, 0],
944: [2, 0, 0, 3]}
95"""
96return {self.__need().index(i): i for i in self.__need()}97
98def main(self, **kwargs) -> None:99"""100Utilize various methods in this class to simulate the Banker's algorithm
101Return: None
102>>> BankersAlgorithm(test_claim_vector, test_allocated_res_table,
103... test_maximum_claim_table).main(describe=True)
104Allocated Resource Table
105P1 2 0 1 1
106<BLANKLINE>
107P2 0 1 2 1
108<BLANKLINE>
109P3 4 0 0 3
110<BLANKLINE>
111P4 0 2 1 0
112<BLANKLINE>
113P5 1 0 3 0
114<BLANKLINE>
115System Resource Table
116P1 3 2 1 4
117<BLANKLINE>
118P2 0 2 5 2
119<BLANKLINE>
120P3 5 1 0 5
121<BLANKLINE>
122P4 1 5 3 0
123<BLANKLINE>
124P5 3 0 3 3
125<BLANKLINE>
126Current Usage by Active Processes: 8 5 9 7
127Initial Available Resources: 1 2 2 2
128__________________________________________________
129<BLANKLINE>
130Process 3 is executing.
131Updated available resource stack for processes: 5 2 2 5
132The process is in a safe state.
133<BLANKLINE>
134Process 1 is executing.
135Updated available resource stack for processes: 7 2 3 6
136The process is in a safe state.
137<BLANKLINE>
138Process 2 is executing.
139Updated available resource stack for processes: 7 3 5 7
140The process is in a safe state.
141<BLANKLINE>
142Process 4 is executing.
143Updated available resource stack for processes: 7 5 6 7
144The process is in a safe state.
145<BLANKLINE>
146Process 5 is executing.
147Updated available resource stack for processes: 8 5 9 7
148The process is in a safe state.
149<BLANKLINE>
150"""
151need_list = self.__need()152alloc_resources_table = self.__allocated_resources_table153available_resources = self.__available_resources()154need_index_manager = self.__need_index_manager()155for kw, val in kwargs.items():156if kw and val is True:157self.__pretty_data()158print("_" * 50 + "\n")159while need_list:160safe = False161for each_need in need_list:162execution = True163for index, need in enumerate(each_need):164if need > available_resources[index]:165execution = False166break167if execution:168safe = True169# get the original index of the process from ind_ctrl db170for original_need_index, need_clone in need_index_manager.items():171if each_need == need_clone:172process_number = original_need_index173print(f"Process {process_number + 1} is executing.")174# remove the process run from stack175need_list.remove(each_need)176# update available/freed resources stack177available_resources = np.array(available_resources) + np.array(178alloc_resources_table[process_number]179)180print(181"Updated available resource stack for processes: "182+ " ".join([str(x) for x in available_resources])183)184break185if safe:186print("The process is in a safe state.\n")187else:188print("System in unsafe state. Aborting...\n")189break190
191def __pretty_data(self):192"""193Properly align display of the algorithm's solution
194"""
195print(" " * 9 + "Allocated Resource Table")196for item in self.__allocated_resources_table:197print(198f"P{self.__allocated_resources_table.index(item) + 1}"199+ " ".join(f"{it:>8}" for it in item)200+ "\n"201)202print(" " * 9 + "System Resource Table")203for item in self.__maximum_claim_table:204print(205f"P{self.__maximum_claim_table.index(item) + 1}"206+ " ".join(f"{it:>8}" for it in item)207+ "\n"208)209print(210"Current Usage by Active Processes: "211+ " ".join(str(x) for x in self.__claim_vector)212)213print(214"Initial Available Resources: "215+ " ".join(str(x) for x in self.__available_resources())216)217
218
219if __name__ == "__main__":220import doctest221
222doctest.testmod()223