//修改资源数 public bool modifyRes(List <Int32> newRes) { if (newRes.Count != resCount.columnNum) { return(false); } //TODO: 判断是否超出了一些进程的Max if (isOverFlow(newRes)) { LogoutEventArgs e = new LogoutEventArgs("error: 修改后不能满足某些进程的max最大需求."); Logout.NewMsg(e); return(false); } //修改Available,先计算出当前的已分配的资源向量,再进行向量加减 List <int> allocationList = getAllocationList(); List <int> result = new List <int>(); for (int i = 0; i < resNum; i++) { result.Add(0); } BankerHelper.VectorRed(newRes, allocationList, ref result); available.modify(result); resCount.modify(newRes); return(true); }
//银行家算法 private List <string> bankerAlg(string name, List <int> request) { //备份数据 List <List <int> > allocationBackup, needBackup; List <int> availBackup; availBackup = available.getDataList(); allocationBackup = allocation.getData(); needBackup = need.getData(); //如果Request大于need,报错 if (BankerHelper.VectorLess(request, need.getVector(name))) { if (BankerHelper.VectorLess(request, available.getDataList())) { //尝试赋值 List <int> newAvail = available.getDataList(); BankerHelper.VectorRed(available.getDataList(), request, ref newAvail); available.modify(newAvail); List <int> newAllocation = allocation.getVector(name); BankerHelper.VectorAdd(allocation.getVector(name), request, ref newAllocation); allocation.modify(name, newAllocation); List <int> newNeed = need.getVector(name); BankerHelper.VectorRed(need.getVector(name), request, ref newNeed); need.modify(name, newNeed); //进行安全性检测 List <string> safetyList = safetyAlg(request); if (safetyList == null) { //恢复数据 LogoutEventArgs a = new LogoutEventArgs("error: 找不到合适的安全序列."); Logout.NewMsg(a); //恢复数据 available.modify(availBackup); allocation.modify(allocationBackup); need.modify(needBackup); } return(safetyList); } else { LogoutEventArgs a = new LogoutEventArgs("error: 尚无足够资源,需要等待."); Logout.NewMsg(a); //恢复数据 available.modify(availBackup); allocation.modify(allocationBackup); need.modify(needBackup); return(null); } } else { LogoutEventArgs a = new LogoutEventArgs("error: 申请的资源数已超过其宣布的最大值(need)."); Logout.NewMsg(a); //恢复数据 available.modify(availBackup); allocation.modify(allocationBackup); need.modify(needBackup); } return(null); }