编程语言
首页 > 编程语言> > 银行家算法模拟

银行家算法模拟

作者:互联网

银行家算法(Banker's Algorithm)是一个避免死锁(Deadlock)的著名算法,是由艾兹格·迪杰斯特拉在1965年为T.H.E系统设计的一种避免死锁产生的算法。它以银行借贷系统的分配策略为基础,判断并保证系统的安全运行。----百度百科

实验要求-----从文件读取数据并输入到文件当中

由于只实现这么一个功能,干脆不分函数了,直接顺下来。

源码po出

  1 available = []#资源向量
  2 proc = []#进程
  3 allocation = []#分配矩阵
  4 max_ = []#最大需求矩阵
  5 need = []#需求矩阵
  6 work = []#工作向量
  7 finish = []
  8 wpa = []#work+allocation
  9 f = open("src.txt","r+")
 10 fr = f.read()
 11 for i in fr:
 12     if i == "\n":
 13         break
 14     if i != " ":
 15         available.append(i)#导入Available
 16 n = len(available)#资源数目
 17 str1 = fr.replace("\n"," ")
 18 list = str1.split(" ")
 19 for i in range(n,len(list),2*n+1):
 20     proc.append(list[i])#导入进程
 21 tmp = 0
 22 temp = n + 1
 23 while temp < len(list):
 24     allocation.append(list[temp])#导入Allocation
 25     tmp+=1
 26     temp+=1
 27     if tmp == n:
 28         tmp = 0
 29         temp += n + 1
 30 temp = 2*n+1
 31 while temp < len(list):
 32     max_.append(list[temp])#导入MAX
 33     tmp+=1
 34     temp+=1
 35     if tmp == n:
 36         tmp = 0
 37         temp += n + 1
 38 for i in range(len(max_)):
 39     need.append(eval(max_[i]) - eval(allocation[i]))#计算need
 40 print("从",proc,"中选择一个请求进程",end=":")
 41 start = input()
 42 print("输入请求的资源(共{a}个,以空格区分)".format(a=n),end=":")
 43 request_t = input()
 44 request = request_t.split(" ")
 45 index = 0
 46 for i in range(len(proc)):
 47     if start == proc[i]:
 48         index = i
 49         break
 50 flag = False
 51 for i in range(n):
 52     if eval(request[i]) > need[i+index*n]:#步骤1判断
 53         flag = False
 54         break
 55     else:
 56         flag = True
 57         continue
 58 if flag == True:
 59     for i in range(n):
 60         if eval(request[i]) > eval(available[i]):#步骤2判断
 61             flag = False
 62             break
 63         else:
 64             flag = True
 65             continue
 66 if flag == True:
 67     for i in range(n):#步骤3修改资源
 68         available[i] = eval(available[i]) - eval(request[i])
 69         allocation[i+index*n] = eval(allocation[i+index*n]) + eval(request[i])
 70         need[i+index*n] = need[i+index*n] - eval(request[i])
 71     work = available
 72     work_str = [0]*n*len(proc)
 73     finish = [False]*len(proc)
 74     for i in range(len(allocation)):
 75         allocation[i] = str(allocation[i])
 76         allocation[i] = eval(allocation[i])
 77     queue = []#安全序列
 78     for i in range(len(proc)):#安全性检查
 79         for index_t in range(len(proc)):
 80             if index_t not in queue:
 81                 if finish[index_t] == False:
 82                     flag_t = True
 83                     for j in range(n):
 84                         if need[j+index_t*n] > work[j]:#判断need[i][j]≤work[j]
 85                             flag_t = False
 86                             break
 87                     if flag_t == True:
 88                         for j in range(n):
 89                             work_str[j+index_t*n] = work[j]
 90                             work[j] = work[j] + allocation[j+index_t*n]
 91                         queue.append(index_t)
 92                         finish[index_t] = True
 93     for i in finish:
 94         if i == False:
 95             flag = False
 96             break
 97     for i in range(len(work_str)):
 98         wpa.append(work_str[i]+allocation[i])
 99     if flag == True:
100         with open("yhj_res.txt","w",encoding="utf-8") as ff:
101             ff.write("\t进程\twork\tallocation\tneed\twork+allocation\tfinish\n")
102             res_list = []
103             for i in queue:
104                 ff.write("\t"+str(proc[i]))
105                 ff.write("\t"+str(work_str[i*n:i*n+n]))
106                 ff.write("\t"+str(allocation[i*n:i*n+n]))
107                 ff.write("\t"+str(need[i*n:i*n+n]))
108                 ff.write("\t"+str(wpa[i*n:i*n+n]))
109                 ff.write("\t   "+str(finish[i])+"\n")
110                 res_list.append(proc[i])
111             ff.write("可以分配,存在安全序列"+str(res_list))
112             print("可以分配,存在安全序列"+str(res_list))
113     else:
114         with open("yhj_res.txt","w",encoding="utf-8") as ff:
115             ff.write("无法分配")
116             print("无法分配")
117     f.close()
118     ff.close()

 

源文件内容---src.txt 

其中,第一行的1 6 2 2表示给出的Available为[1,6,2,2],后面5行的第1列表示进程的名称,根据Available的长度为4,后四列为Allocation,最后4列为Max。

 
1 6 2 2
 
a 0 0 3 2 0 0 4 4
 
b 1 0 0 0 2 7 5 0
 
c 1 3 5 4 3 6 10 10
 
d 0 3 3 2 0 9 8 4
 
e 0 0 1 4 0 6 6 10

 

测试

输出结果到文件yhj_res.txt

进程 work        allocation      need     work+allocation   finish
 
a [1, 5, 1, 2] [0, 0, 3, 2] [0, 0, 1, 2] [1, 5, 4, 4] True
 
d [1, 5, 4, 4] [0, 4, 4, 2] [0, 5, 4, 2] [1, 9, 8, 6] True
 
e [1, 9, 8, 6] [0, 0, 1, 4] [0, 6, 5, 6] [1, 9, 9, 10] True
 
b [1, 9, 9, 10] [1, 0, 0, 0] [1, 7, 5, 0] [2, 9, 9, 10] True
 
c [2, 9, 9, 10] [1, 3, 5, 4] [2, 3, 5, 6] [3, 12, 14, 14] True
 
可以分配,存在安全序列['a', 'd', 'e', 'b', 'c']

 

标签:index,work,len,allocation,算法,str,True,模拟,银行家
来源: https://www.cnblogs.com/WXjzc/p/16066563.html