參考文章?
強烈建議看會思路,代碼倒是可以不看
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._allocation = allocation
self._need = need
self._max = max
self._available = available
self._work = work
self._finish = [False, False, False, False]
# 打印所有數據
def _printAll(self):
print('_allavailable:' + str(self._allavailable))
print('_allocation' + str(self._allocation))
print('_need' + str(self._need))
print('_max' + str(self._max))
print('_available' + str(self._available))
# print('_work' + str(self._work))
print('_finish' + str(self._finish))
# 外部調用
def toWork(self):
#
# print(self._max)
# self._printAll()
# 是否符合銀行家算法的規範
if not self._brank():
print('進制執行不符合銀行家算法,系統不給予資源')
else:
# 是否符合安全行算法
if not self._safe():
print('進程執行不安全,系統不能給予資源')
else:
print('進程可以執行,其執行順序是:' + str(self._SecuredSequence))
# todo 實現銀行家算法,符合返回true,反之false
def _brank(self):
request = [1, 0, 0, 0, 1]
# request = []
# for i in range(self._types):
# a = int(input("請輸入request" + str(i) + "值: "))
# request.append(a)
# 1)如果Request i[j]≤Need[i, j],轉向步驟(2);否則所需資源數已超過它所宣佈的最大值。
if not self._requestSmall(request, self._need[self._useProcess]):
return False
else:
# 2)如果Requesti[j]≤Available[i, j],轉向步驟(3);否則無足夠資源,Pi需等待。
if not self._requestSmall(request, self._available):
return False
else:
# 系統試探着把資源分配給進程Pi,並修改下面數據結構中的數值:
# Allocation[i, j]=Allocation[i, j]+Requesti[j];
# Need[i, j]=Need[i, j]-Requesti[j];
self._available = self._subtraction(self._available, request)
self._need[self._useProcess] = self._subtraction(
self._need[self._useProcess], request)
self._allocation[self._useProcess] = self._add(
self._allocation[self._useProcess], request)
print('修改可利用資源向量爲:' + str(self._available))
print('修改需求矩陣爲:' + str(self._need[self._useProcess]))
print('修改分配矩陣爲:' + str(self._allocation[self._useProcess]))
return True
# todo 實現安全性算法,安全返回true,反之false
def _safe(self):
count = 0
while count < self._processes:
flag = False # 一次循環中找不到安全的狀態,直接變爲不安全,結束循環
for i in range(self._processes):
if not self._finish[i]: # 只會去從沒有執行過的地方執行
if count == 0: # 開始的時候Work=Available
if self._requestSmall(self._need[i], self._available):
# print(self._need[i])
self._finish[i] = True # 告訴程序此進程可以通過安全算法
self._work = self._add(self._available,
self._allocation[i]) # Work[j]=Work[j]+Allocation[i, j];
count += 1
# print(self._work)
# print(i)
print(
'進程號\tWord\t\tNeed\t\tAllocation\tWork+Allocation\tFinish')
print(str(i)+'\t'+str(self._subtraction(self._work, self._allocation[i]))+'\t'+str(
self._need[i])+'\t'+str(self._allocation[i])+'\t'+str(self._work)+'\t'+str(self._finish[i]))
flag = True
self._SecuredSequence.append(i) # 記錄安全序列的值
else:
if self._requestSmall(self._need[i], self._work):
self._finish[i] = True # 告訴程序此進程可以通過安全算法
self._work = self._add(self._work, # debug
self._allocation[i]) # Work[j]=Work[j]+Allocation[i, j];
count += 1
flag = True
# print(self._work)
# print(i)
print(str(i)+'\t'+str(self._subtraction(self._work, self._allocation[i]))+'\t'+str(
self._need[i])+'\t'+str(self._allocation[i])+'\t'+str(self._work)+'\t'+str(self._finish[i]))
self._SecuredSequence.append(i) # 記錄安全序列的值
if not flag:
return False
return True
# 比較兩個列表每個的大小,第一個列表值都小於第二個列表的值返回ture,反之返回flase
def _requestSmall(self, request, others):
for i in range(self._types):
if request[i] > others[i]:
return False # request 列表的元素有大於第二個的,直接結束
return True
# 返回兩個列表每個元素相減的值
def _subtraction(self, list1, list2):
mylist = []
for i in range(self._types):
mylist.append(list1[i] - list2[i])
return mylist
# 返回兩個列表每個元素相加的值
def _add(self, list1, list2):
mylist = []
for i in range(self._types):
mylist.append(list1[i] + list2[i])
return mylist
demo = Bankers_Algorithm()
demo.toWork()