首页 > 编程 > Python > 正文

银行家算法的Python简单实现

2019-11-09 19:32:40
字体:
来源:转载
供稿:网友

写的比较挫,注释给的比较详细,新手可以看看。

import copyimport rePRoc = dict()def comp(x,y): #x为need,y为available flag=1 for i in range(len(x)): # 若出现need大于available,则flag置0 if x[i] > y[i]: flag=0 return flag#***************************字段录入*******************************print "Please input the number of resource kind"rec=int(raw_input())num=recprint "Please input process's names each in 1 character:"rec=raw_input()Pname = re.findall('[a-zA-Z]',rec) #将名字存入该listfor x in range(len(Pname)): print "Please input ",num," resources Max value for process "+Pname[x]+":" rec = raw_input() L1 = re.findall('[0-9]+$|[0-9]', rec) # Max列表 L1 = [int(i) for i in L1] if len(L1)!=num: print "Input value error,the numeber of resource is less than expected." exit() print "Please input ",num," resources Allocation value for process "+Pname[x]+":" rec = raw_input() L2 = re.findall('[0-9]+$|[0-9]', rec) # Allocation列表 L2 = [int(i) for i in L2] if len(L2) != num: print "Input value error,the numeber of resource is less than expected." exit() for i in range(num): # 若出现Max小于Allocation if L1[i] < L2[i]: print "Input value error,the value of Max is smaller than allocation!" exit() L3 = [L1[i] - L2[i] for i in range(num)] # Neede列表 L = [L1, L2, L3] # P1的键值 proc[Pname[x]] = Lprint " Max Allocation Need"for i in range(len(Pname)): print Pname[i],proc[Pname[i]]print "Please input avaliable resources:"rec=raw_input()A = re.findall('[0-9]+$|[0-9]', rec) #Available列表A = [int(i) for i in A]if len(A) < num: print "Input value error,the numeber of resource is less than expected." exit()Path=[] #初始化Index=[]Alloc=[[0,0,0]] #需要对alloc赋初值,否则顶层alloc为空N=[] #记录路径数# # A_tem=A# # Path_tem=Path# # Index_tem=Index# Alloc_tem=[[0,0,0]]# # Pname_tem=Pname# N_tem=N#***************************核心算法*******************************def bank(p, a_f, proc_f,path,index,alloc,n,q): #p为进程的name列表,a_f为可用资源,proc_f为键值为三矩阵的字典 if len(p)>=1: for x in p: if comp(proc_f[x][2],a_f)==1: a_f = [a_f[i] + proc_f[x][1][i] for i in range(q)] alloc.append(proc_f[x][1]) #将获得的allocation值保存 path.append(x) #满足条件则获得该节点路径 index.append(p.index(x)) #获得x的索引值用于还原 p.remove(x) #若满足条件则从列表中移除x bank(p, a_f, proc_f, path, index, alloc, n,q) if x==p[len(p)-1]: #当一个循环遍历至最后一个元素时还原p的配置 if path==[]: #当弹回顶层时,path会为空,需特殊处理 break #该层循环遍历结束后break else: x=path.pop() #恢复上层遍历的x的值 p.insert(index.pop(), x) alloc_tem=alloc.pop() #取出上层循环所得资源值 a_f = [a_f[i] - alloc_tem[i] for i in range(q)] #恢复上层遍历的可用资源值 break #该层循环遍历结束后break elif len(p)==0: print path n.append('1') p.append(path.pop()) index.pop()# print "Safe path: "bank(Pname, A, proc, Path, Index, Alloc, N, num)N=len(N)if N>1: print "There're",N, "ways in total."elif N==1: print "There's only",N, "way."else: print "There's no way out there. This is not a safe system, and here's an example of a safe system as follow:"#若不安全则直接退出程序 Path = [] # 初始化 Index = [] Alloc = [[0, 0, 0]] # 需要对alloc赋初值,否则顶层alloc为空 N = [] # 记录路径数 A=[i+1000 for i in A] print "avaliable resorces:",A bank(Pname, A, proc, Path, Index, Alloc, N, num) N_tem=len(N) print "There're",N_tem, "ways in total." exit()#***************************************进程提出需求***********************************************while len(Pname)>0: while 1: print "Continue?<Y/N>" rec = raw_input() if rec=="N": exit() elif rec=="Y": break else: print "Inpu error." Path = [] # 初始化 Index = [] Alloc = [[0, 0, 0]] # 需要对alloc赋初值,否则顶层alloc为空 N = [] # 记录路径数 print "Please input request process:" rec=raw_input() Rname = re.findall('[a-zA-Z]',rec) #将名字存入该list if len(Rname)!=1: #若变量数不为1报错 print "Input error, inputting more process than expected." continue #则重新输入 else: Rname=Rname[0] #由list变为字符串 print "Please input request resource:" rec = raw_input() LR = re.findall('[0-9]+$|[0-9]', rec) # Request列表 LR = [int(i) for i in LR] if len(LR) != num: #资源数不符 print "Input value error,the numeber of resource is less than expected." continue #则重新输入 else: if comp(LR,A)==0: #若avaliable小于request,则重新输入request print "Input value error,the value of avaliable is smaller than request!" continue if LR==proc[Rname][2]: #若request恰好等于need,运行完成后应弹出相应的process for x in range(num): proc[Rname][2][x] = proc[Rname][2][x] - LR[x] #需求减少 proc[Rname][1][x] = proc[Rname][1][x] + LR[x] #alloc增加 A[x] = A[x] - LR[x] bank(Pname, A, proc, Path, Index, Alloc, N, num) N = len(N) if N > 1: print "There're", N, "ways in total." for x in range(num): A[x]=A[x]+proc[Rname][0][x] #将该进程占有的资源收回 proc.pop(Rname) Pname.remove(Rname) elif N == 1: print "There's only", N, "way." for x in range(num): A[x]=A[x]+proc[Rname][0][x] #将该进程占有的资源收回 proc.pop(Rname) Pname.remove(Rname) else: print "There's no way out there" # 若不安全则不对资源进行操作,回滚 for x in range(num): proc[Rname][2][x] = proc[Rname][2][x] + LR[x] # 回滚 proc[Rname][1][x] = proc[Rname][1][x] - LR[x] # 回滚 A[x] = A[x] + LR[x] else: #若request不等于need,运行完成后应弹出相应的process for x in range(num): proc[Rname][2][x] = proc[Rname][2][x] - LR[x] # 需求减少 proc[Rname][1][x] = proc[Rname][1][x] + LR[x] # alloc增加 A[x]=A[x]-LR[x] bank(Pname, A, proc, Path, Index, Alloc, N, num) N = len(N) if N > 1: print "There're", N, "ways in total." elif N == 1: print "There's only", N, "way." else: print "There's no way out there" # 若不安全则不对资源进行操作,回滚 for x in range(num): proc[Rname][2][x] = proc[Rname][2][x] + LR[x] # 回滚 proc[Rname][1][x] = proc[Rname][1][x] - LR[x] # 回滚 A[x] = A[x] + LR[x] print " Max Allocation Need" for i in range(len(Pname)): print Pname[i], proc[Pname[i]] print "Avaliable resources: ",Aprint "/nAll process are completed."
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表