# 银行家算法vue实现 **Repository Path**: lhyu/bank-algorithm-vue ## Basic Information - **Project Name**: 银行家算法vue实现 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-11-26 - **Last Updated**: 2022-01-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 银行家算法vue实现 > 说明:参照大佬的h5实现银行家算法,我在这用vue实现了一版,以下是大佬链接【https://blog.csdn.net/sinat_34260423/article/details/51707166】向大佬致敬 ## 算法简介 银行家算法是最著名的**死锁避免**算法,其**思想**是:把操作系统视为银行家,操作系统管理的资源相当于银行家管理的资金,进程向操作系统请求分配资源相当于用户向银行家贷款。操作系统按照银行家制定的规则为进程分配资源。进程运行之前先声明对各种资源的最大需求量,当进程在执行中继续申请资源时,先测试该进程已占用的资源数与木次申请的资源数之和是否超过该进程声明的最大需求量。若超过则拒绝分配资源,若未超过则再测试系统现存的资源能否满足该进程尚需的最大资源量,若能满足则按当前的申请量分配资源,否则也要推迟分配。 ### 王道书截图 ![image-20211203213249446](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213249446.png)![image-20211203213301429](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213301429.png)![image-20211203213315870](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213315870.png)![image-20211203213330503](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213330503.png)![image-20211203213339469](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213339469.png)![image-20211203213352850](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213352850.png)![image-20211203213400271](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213400271.png)![image-20211203213412422](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203213412422.png) ### 流程图 ![image-20211203221317581](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211203221317581.png) ## 代码分析 ### 所需数据(data中数据) ```js data() { return { num_process: '', //记录进程数 num_resource: '', //记录资源数 process: 1, // 当前进度 availableTitle: [], // 可利用资源数目 available: [], // 可利用资源数目 // isb_ok2: false, // 按钮是否可用 helpObjArr: [], // 辅助对象数组[{name:'',max:[],available:[],allocation:[],need:[]}] fg: false, //更新Available标志 work: [], //资源可用数 work2: [], //用于记录每次进程调用的Work数 finish: [], //是否已完成 o: 0, safe: [], //安全序列 safeStr: '', // 展示安全序列,不安全则提出警告 showTable2: false, // request: [],//请求资源数 helpObjArr2: [],// 辅助对象数组[{name:'',work:[],need:[],allocation:[],work+allocation:[],finish:false}] showSafeTable: false, // 是否展示安全表格,可得到安全序列则展示 } }, ``` ### 进程和资源输入页 没啥好说的,通过这俩变量记录num_process,num_resource ![image-20211204093702652](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211204093702652.png) 点击确定按钮增加了校验,点击确定后进入第二阶段,process赋值为2 ```js onClickOK() { if(this.num_process && this.num_resource){ this.$confirm(`${this.num_process}个进程${this.num_resource}个资源`, { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { this.process = 2; // 隐藏初试页面,进入第二个步骤 }).catch(() => { }); }else{ this.$message({ message: '请输入数字', type: 'warning' }); } }, ``` ![image-20211204094039157](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211204094039157.png) ![image-20211204094152088](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211204094152088.png) 输入对应资源数目,进入process=3![image-20211204094345304](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211204094345304.png) ### 生成第一个表格 页面 ![image-20211204094532151](C:\Users\LHY\AppData\Roaming\Typora\typora-user-images\image-20211204094532151.png) 重点在于表格内容分配上,需要合并单元格,并且进程资源数目是根据所输入num_resource大小所决定的,行数则和num_process的大小息息相关,我这里采用了helpObjArr来存储表格内容 helpObjArr数据格式[{name:'',max:['','',''],available:['','',''],allocation:['','',''],need:['','','']}] 其中name是进程名称,以max为例:max数组中存储的是各个进程资源,如max[0]对应max中A资源数目 将表格内容划分为两部分:资源行和其余的行 ```html
资源 Max Allocation Need Available
{{items.name}}
``` 生成helpObjArr的代码 ```js for (let i=0;i<= this.num_process;i++) { let arr = [] let maxArr = [] let availableArr = [] let allocationArr = [] let needArr = [] for (let ii=0;ii0) { for(let i in this.helpObjArr[item].max){ this.helpObjArr[item].need[i] = this.helpObjArr[item].max[i] - this.helpObjArr[item].allocation[i] } } } // 触发刷新 this.$forceUpdate() }, ```