银行家算法的python代码实现,感觉python写算法简直要起飞

上操作系统课,学到死锁,老师讲了银行家算法,正好在图书馆,用了1个多小时写出来,用python写算法发现自己最大的问题是有些语法一直学的不是很深入,查了半天函数的用法啊喂!!!坑爹,具体算法相信大家都懂,就不和大家赘述了,代码可能会有些bug,功能也不是很完善,希望大家给出批评指正!

import numpy as np
avaResour = np.array([3,3,2])
allocation = np.array([[0,1,0],[2,0,0],[3,0,2],[2,1,1],[0,0,2]])
maxRequest = np.array([[7,5,3],[3,2,2],[9,0,2],[2,2,2],[4,3,3]])
need = np.array([[7,4,3],[1,2,2],[6,0,0],[0,1,1],[4,3,1]])
askResource=np.array([0,0,0])

def BankerAllgorithm():
    global avaResour,allocation,maxRequest,need,askResource
    print "thread num 5,res num 3"
    print "res available :"
    for x in avaResour:
        print x
    print("the max request :")
    for x in maxRequest:
        print x
    print "allocation :"
    for  x in allocation:
        print x
    print "need :"
    for x in need:
        print x    

    RequestThreadNum = input("enter the request thread number :from 0 -> 4 : ")
    #test RequestThreadNum is ok
    while RequestThreadNum>4 or RequestThreadNum<0:
        print("please enter the correct number ! From 0 - 4 : ")
        RequestThreadNum = input("enter the number : ")

   #input
    print "please enter number of resources,the format is x x x : "
    for x in xrange(0,3):
        askResource[x] = input()

    if askResource[x]<=need[RequestThreadNum][x]:
        print("Bankalgorithm is running ...biubiubiu...waiting...")
        if askResource[x]<=avaResour[x]:
            avaResour =avaResour - askResource
            allocation = allocation + askResource
            need[RequestThreadNum] = need[RequestThreadNum] - askResource
            print "Trial allocation is ok !"
            if (safeTest()):
                print "begin to allocation res to "+RequestThreadNum 
                print "allocation is ok,res has been refreshed"
                for x in xrange(0,5):
                    if  need[x]==0:
                        for y in xrange(0,3):
                            avaResour[y] = avaResour[y] + allocation[x][y]
                            allocation[x][y] = 0                                
            else:
                print "Trial allocation is wrong ,we are trying to get back to last statue!"
                avaResour = avaResour + askResource
                allocation = allocation - askResource
                need = need +askResource
        else:
            print "there is not enough resource now , try to wait for sometime!"
    else:
        print "error ! askResource is bigger than need"


def safeTest():   #安全性检查 
    global avaResour,allocation,maxRequest,need,askResource
    s=0;m=0;z=0;r=[0,0,0,0,0];y=0;work=[0,0,0];finish = [0,0,0,0,0]
    print "enter safety test ..."
    work = avaResour
    for x in xrange(0,5):
        for y in xrange(0,5):
            if finish[y]==0 and need[y][0]<=work[0] and need[y][1]<=work[1] and need[y][2]<=work[2]:
                for z in xrange(0,3):
                    work[z] = work[z] + allocation[y][z]
                finish[y] =1;r[y] =s;
                y +=y

    if y==5:
        print "find a safety order"
        for x in xrange(0,3):
            print "P " +r[2*x]+"-->"+" P "+r[2*x +1]+"-->"
        print " P "+ r[4] +" pass safety test"
        return 1
    else:
        return 0

if __name__ == '__main__':
    BankerAllgorithm()
    原文作者:银行家问题
    原文地址: https://blog.csdn.net/ydjcs567/article/details/52821986
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞