银行家算法就是避免死锁。每次分配资源后检查是否存在能够存在一个平稳结束的序列。 不会引起死锁。 思想就是这么简单。。代码写的一般。。忽略~~~
#coding=utf-8
import random,time
def generateName():
global name
t=name
name=chr(ord(name)+1)
return t
class Resource:
def __init__(self,tname,tnumber):
self.name=tname
self.number=tnumber
def init():
global n,name,p
############################################################
n=raw_input('how many Resource ? (2~4 suggested):')
n=int(n,10)
print 'n=',n
###############
name='a'
############################################################
p=raw_input('how many process ?(1~3 suggested):')
p=int(p,10)
print 'p=',p
def initDS():
global n,p,Available,Max,Allocation,Need,Finish
#produce Available matrix
Available=[]
for i in xrange(n):
Available.append(Resource(tname=generateName(),tnumber=random.randint(10,20)))
print 'Generate Resource: name=%s,resource=%d'%(Available[i].name,Available[i].number)
#produce Max matrix
Max=[]
#a process need a resource <= 6
print '-------------------------------------'
print 'Max matrix:'
for i in range(p):
temp=[]
for j in range(n):
temp.append(random.randint(0,6))
Max.append(temp)
print 'process %d need resource '%i,temp
del temp
#produce Allocation
print '-------------------------------------'
print 'Allocation matrix'
Allocation=[]
for i in range(p):
temp=[]
for j in range(n):
temp.append(0)
Allocation.append(temp)
print 'process %d get resource '%i,temp
del temp
#produce Need matrix
print '-------------------------------------'
print 'Need matrix:'
Need=[]
for i in range(p):
temp=[]
for j in range(n):
temp.append(Max[i][j]-Allocation[i][j])
Need.append(temp)
print 'process %d need resource '%i,temp
del temp
print '-------------------------------------'
Finish=[]
for i in range(p):
Finish.append(False)
def safeCheck(targetProcess,targetResource):
#
global n,p,Available,Max,Allocation,Need,Finish
#print 'in the safe Checking!'
#print 'work:'
work=[]
#copy Available to work
for i in range(n):
work.append(Available[i].number)
#print work
finish=[]
for i in range(p):
finish.append(Finish[i])
#print finish
#print 'need:'
need=[]
for i in range(p):
temp=[]
for j in range(n):
temp.append(Need[i][j])
need.append(temp)
#print temp
del temp
#---------------------------------
#---------------------------------
s=0 # s from 0 --- p-1
count=0
while True:
#print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
#print '>>>>>>count=%d>>s=%d>>>>>>'%(count,s)
alldone=1
for i in range(p):
if finish[i]==False:
alldone=0
break
if alldone==1:
#print 'alldone'
return True
if(count>p):
print '[-] Dead Lock !'
return False
break
if finish[s]==True:
count+=1
s=(s+1)%p
continue
tflag=1
for i in range(n):
if need[s][i]>work[i]:
tflag=0
if tflag==1:#have enough resource
#do the job and release the resource
#release
#print 'doing s=%d'%s
for i in range(n):
work[i]+=Allocation[s][i]
finish[s]=True
'''
print 'test in the safe checking !'
print s,'Done!'
print 'work',work
print 'finish',finish
'''
count=0
#print 'finish %d and count=%d -------------------'%(s,count)
else:
#don nothing
count+=1
#print 'can\'t do the %d and count =%d'%(s,count)
s=(s+1)%p
#------------------------------------------------------------------------------
def query():
global n,p,Available,Max,Allocation,Need,Finish
#Resource
print '-------------------------------------'
for i in xrange(n):
print 'Available Resource %s,resource=%d'%(Available[i].name,Available[i].number)
print '-------------------------------------'
print 'Max matrix:'
for i in range(p):
print 'process %d need resource '%i,Max[i]
#produce Allocation
print '-------------------------------------'
print 'Allocation matrix:'
for i in range(p):
print 'process %d get resource '%i,Allocation[i]
#produce Need matrix
print '-------------------------------------'
print 'Need matrix:'
for i in range(p):
print 'process %d need resource '%i,Need[i],
if Finish[i]==True:
print 'Done !'
else:
print ''
print '-------------------------------------'
for i in range(p):
print '%d\t'%i,
print
for i in range(p):
print Finish[i],'\t',
print
print '-------------------------------------'
if __name__=='__main__':
init();
initDS();
global n,p,Available,Max,Allocation,Need,Finish
while True:
print '-------------------------------------'
print
print
print
print '-------------------------------------'
c=raw_input('Query(q)/Allocate(a)/Exit(e)?\n>')
#c=lower(c)
print 'you input ',c
#print process
if c=='q':
query()
elif c=='a':
#Allocate
flag=1
print "There are %d process : (0~%d)"%(p,p-1)
tn=raw_input('Allocate resource for which process: ')
#raw_input
tn=int(tn,10)
if tn>p or tn==p:
print 'no processs!'
continue
print 'there are %d resources:'%n
ta=[]
for i in range(n):
t=raw_input('Allocate Resource %s (need %d , access %d): '%(Available[i].name,Need[tn][i],Available[i].number))
t=int(t,10)
if t>Available[i].number:
print 'we can\'t get so much resources,we use %d instead of %d'%(Available[i].number,t)
t=Available[i].number
if t>Need[tn][i]:
print 'you input too much resource and we use %d instead of %d'%(Need[tn][i],t)
t=Need[tn][i]
ta.append(t)
del t
print 'You requests to allocate process',tn,'Resource',ta
print 'Checking!-----------'
for i in range(n):
Available[i].number-=ta[i]
Allocation[tn][i]+=ta[i]
Need[tn][i]-=ta[i]
print 'Safe Checking!---------------------'
if(safeCheck(tn,ta)):
print 'succeed allocate the resource'
tndone=1
for i in range(n):
if Need[tn][i]>0:
tndone=0
break
if tndone==1:
Finish[tn]=True
#for i in range(n):print Available[i].number,
#print
for i in range(n):
Available[i].number+=ta[i]
Allocation[tn][i]=0
Need[tn][i]=0
#for i in range(n):print Available[i].number,
#print
alldone=1
for i in range(p):
if Finish[i]==False:
alldone=0
break
if alldone==1:
print '-------------------------------------'
print 'Need matrix:'
for i in range(p):
print 'process %d need resource '%i,Need[i],
if Finish[i]==True:
print 'Done !'
else:
print ''
print '-------------------------------------'
print 'ALL DONE!'
break
else:
print 'it\'s dangerous! we have cancel it'
#and release the error resoruces
for i in range(n):
Available[i].number+=ta[i]
Allocation[tn][i]-=ta[i]
Need[tn][i]+=ta[i]
print '-------------------------------------'
for i in xrange(n):
print 'Available Resource %s,resource=%d'%(Available[i].name,Available[i].number)
print '-------------------------------------'
print 'Need matrix:'
for i in range(p):
print 'process %d need resource '%i,Need[i],
if Finish[i]==True:
print 'Done !'
else:
print ''
print '-------------------------------------'
elif c=='e':
break
else:
print '[-]Input Error !'
#print Resource
print 'All Done and Exit succeed!'
基本没有什么大问题。运行结果太长。就不贴上来了。。。