参考文章?
强烈建议看会思路,代码倒是可以不看
class Bankers_Algorithm:
_flag = True
_count = 0
_processes = 4 # 进程数
_types = 5 # 资源种类
_SecuredSequence = [] # 安全序列
_useProcess = 0
_allavailable = None # 五种种资源的数量
_allocation = None # 分配矩阵
_need = None # 需求矩阵
_max = None # 最大需求矩阵
_available = None # 可利用资源向量
_work = None
_finish = None
# 初始化所有数据化
def __init__(self):
allavailable = [5, 6, 8, 6, 4]
allocation = [[0, 2, 1, 1, 1], [2, 0, 1, 1, 1],
[0, 1, 0, 1, 1], [0, 3, 1, 2, 0]]
need = [[1, 0, 2, 1, 1], [0, 3, 2, 1, 0],
[0, 3, 3, 2, 2], [1, 0, 1, 2, 1]]
available = [0, 0, 0, 0, 0]
max = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
work = [0, 0, 0, 0, 0]
temp = [0, 0, 0, 0, 0]
for i in range(self._processes):
for j in range(self._types):
temp[j] += allocation[i][j]
# 银行家之间算法的关系:Need[i, j]=Max[i, j]-Allocation[i, j]
max[i][j] = allocation[i][j] + need[i][j]
for i in range(self._types):
available[i] = allavailable[i] - temp[i]
self._allavailable = allavailable
self._allo