操作系统实验
----------------------------------------------------------------该代码亲试有效,欢迎各位dalao对代码不足处,提出建议,留言评论。
----------------------------------------------------------------本人所用编译器python3.5
----------------------------------------------------------------本人QQ:2149810847
'''
目的:
熟悉银行家算法,加深死锁有关概念的理解。
内容:
编制银行家算法通用程序,并检测思考题中所给状态的安全性。
要求:
(1) 下列状态是否安全?(三个进程共享12个同类资源)
进程 已分配资源数 最大需求数
1 1 4 (状态a)
2 4 4
3 5 8
1 1 4
2 4 6 (状态b)
3 6 8
(2) 考虑下列系统状态
分配矩阵 最大需求矩阵 可用资源矩阵
0 0 1 2 0 0 1 2 1 5 2 0
1 0 0 0 1 7 5 0
1 3 5 4 2 3 5 6
0 6 3 2 0 6 5 2
0 0 1 4 0 6 5 6
问系统是否安全?若安全就给出所有的安全序列。若进程2请求(0420),可否立即分配?
'''
#银行家算法
'''
有效的资源 Avaliable[j]
最大需求 Max[i,j]
已分配的资源 Allocation[i,j]
资源需求 Need[i,j]
'''
from numpy import *
from itertools import permutations
Finish = []
Work = []
order = {}
#读文件
def readfile(filename):
file = open(filename)
dataSet = []
for line in file.readlines():
curLine = line.strip().split("\t")
floatLine = list(map(float, curLine)) # map()
#print(floatLine)
dataSet.append(floatLine)
#print(dataSet)
return dataSet
#比较某一个进程的需求和有效的资源
def need_work(need, work):
for j in range(len(need)): #number_of_resources
if need[j] > work[j]:
return False
return True
#2List add
def add_list(list1, list2):
for i in range(len(list1)):
list1[i] = list1[i] + list2[i]
return list1
'''
def distribute(Need,Finish,Work): # start parameter
list = []
for i in range(number_of_processes):
if (Finish[i][1] == False) and (need_work(Need[i], Work[0])):
#Work[0] = add_list(Work[0], Allocation[i])
#Finish[i][1] = True
list.append(i+1)
return list
def Banker(number,Need,Finish,Work,Allocation,dict):
if number == 0:
return
list = distribute(Need,Finish,Work)
for value in list:
Finish[value-1][1] = True
Work[0] = add_list(Work[0], Allocation[value-1])
number -= 1
print(value)
Banker(number,Need,Finish,Work,Allocation,dict)
'''
def safe_list(number_of_processes):
l ,result = [], []
for i in range(number_of_processes):
l.append(i+1)
for j in permutations(l, len(l)):
result.append(list(j))
return result
def banker(l,Need,Finish,Work,Allocation):
n, f, w, a = Need.copy(), Finish.copy(), Work[0].copy(), Allocation.copy()
for i in range(len(l)):
if (f[l[i]-1] == False) and (need_work(n[l[i]-1], w)):
w = add_list(w, a[l[i]-1])
f[l[i]-1] = True
else:
return False
return True
if __name__ == '__main__':
maybe_safe_list = []
Allocation = readfile("Allocation.txt")
Max = readfile("Max.txt")
Need = readfile("Need.txt")
Avaliable = readfile("Avaliable.txt")
Work = Avaliable
#print(Allocation)
#print(Max)
#print(Need[1],Need)
#print(Avaliable)
#print(Work)
number_of_resources = len(Allocation[0])
#global number_of_processes
number_of_processes = len(Allocation[:])#print(number_of_resources,number_of_processes)
for k in range(number_of_processes):
Finish.append(False)
#print(finish,work) #[[1, False], [2, False], [3, False], [4, False], [5, False]] []
#distribute(Need,Finish,Work)
maybe_safe_list = safe_list(number_of_processes)
#print(maybe_safe_list)
#b=banker([2,4,1,3,5],Need,Finish,Work,Allocation)
#print(b)
#print(Finish)
for i in range(len(maybe_safe_list)):
#print(maybe_safe_list[i])
if banker(maybe_safe_list[i],Need,Finish,Work,Allocation):
print(maybe_safe_list[i])
#print(Work)
#print(Finish)
#print(1)