银行家算法的python代码实现,感觉python写算法简直要起飞
来源:互联网 发布:mac 如何恢复默认dns 编辑:程序博客网 时间:2024/05/29 19:26
上操作系统课,学到死锁,老师讲了银行家算法,正好在图书馆,用了1个多小时写出来,用python写算法发现自己最大的问题是有些语法一直学的不是很深入,查了半天函数的用法啊喂!!!坑爹,具体算法相信大家都懂,就不和大家赘述了,代码可能会有些bug,功能也不是很完善,希望大家给出批评指正!
import numpy as npavaResour = 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 0if __name__ == '__main__': BankerAllgorithm()
1 0
- 银行家算法的python代码实现,感觉python写算法简直要起飞
- 银行家算法的Python简单实现
- 银行家算法的java代码实现,Swing写的界面
- python模拟银行家算法
- 银行家算法的实现
- 银行家算法的实现
- 银行家算法的模拟实现
- 银行家算法的C实现
- 银行家算法的C++实现
- 银行家算法的简单实现
- C_银行家算法的实现
- [C#]银行家算法的实现
- 操作系统: 银行家算法的分析和代码实现(c++语言)
- python实现的各种排序算法代码
- python实现的各种排序算法代码
- 用c++写银行家算法的感想
- 银行家算法模拟代码
- KNN算法--python实现代码
- 异常的应用
- JMeter压力测试案例大全 之(邮件服务器测试和元件介绍)
- 解决Ubuntu6.04系统sublime text3无法输入搜狗中文
- I/O模型: 阻塞、非阻塞、I/O复用、同步、异步
- 【并行计算-CUDA开发】有关CUDA当中global memory如何实现合并访问跟内存对齐相关的问题
- 银行家算法的python代码实现,感觉python写算法简直要起飞
- 全局变量、局部变量、静态全局变量、静态局部变量的区别
- Spatial Pyramid Pooling in Deep Convolutional Networks for Visual
- 设计师应该学会的Sketch 2 中英文对照版的快捷键
- iOS学习之键盘设置以及IQKeyboardManager
- 【并行计算-CUDA开发】CUDA存储器模型
- js基础学习
- Spring 源码阅读一 环境搭建
- 第七周 项目1-建立顺序串的算法库