//修改资源数 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 void bt_rsRequest_Click(object sender, RoutedEventArgs e) { if (banker.applyForRes(cb_progName.Text, requestManager.getVector())) { LogoutEventArgs a = new LogoutEventArgs("success: 申请资源成功."); Logout.NewMsg(a); } else { LogoutEventArgs a = new LogoutEventArgs("申请资源失败."); Logout.NewMsg(a); } }
private void bt_addRes_Click(object sender, RoutedEventArgs e) { if (banker.getResNum() >= 26) { LogoutEventArgs a = new LogoutEventArgs("error: 最多支持26个种类的资源"); Logout.NewMsg(a); return; } newProgMax.newColumn(); request.newColumn(); banker.newRes(); tb_resCount.Text = ((Convert.ToInt32(tb_resCount.Text) + 1).ToString()); }
private void bt_updateAvaliable_Click(object sender, RoutedEventArgs e) { List <int> newRes = resCountManager.getVector(); if (banker.modifyRes(newRes)) { LogoutEventArgs a = new LogoutEventArgs("success: 资源已更新."); Logout.NewMsg(a); } else { LogoutEventArgs a = new LogoutEventArgs("error: 资源更新失败."); Logout.NewMsg(a); } }
private void newProgress_Click(object sender, RoutedEventArgs e) { if (tb_newProgName.Text == "") { LogoutEventArgs a = new LogoutEventArgs("error: 进程名为空"); Logout.NewMsg(a); return; } List <int> progMax = newProgMaxManager.getVector(); if (banker.newProgress(tb_newProgName.Text, progMax)) { cb_progName.ItemsSource = null; cb_progName.ItemsSource = banker.getProgressNameList(); tb_progressNum.Text = ((Convert.ToInt32(tb_progressNum.Text) + 1).ToString()); tb_newProgName.Clear(); } }
public bool applyForRes(string name, List <int> request) { //清空workAllocation workAllocation.clearAll(); List <string> safetyList = bankerAlg(name, request); if (safetyList != null) { StringBuilder sb = new StringBuilder("安全序列:{"); foreach (string i in safetyList) { sb.Append(i + ","); } sb.Remove(sb.Length - 1, 1); sb.Append("}"); LogoutEventArgs e = new LogoutEventArgs(sb.ToString()); Logout.NewMsg(e); return(true); } return(false); }
public virtual bool newRow(string header) { if (progressName.Contains(header)) { LogoutEventArgs e = new LogoutEventArgs("error: 已存在同名进程,创建失败."); Logout.NewMsg(e); return(false); } else { progressName.Add(header); List <Int32> t = new List <int>(); for (int i = 0; i < columnNum; i++) { t.Add(0); } data.Add(t); row++; onDataChanged(null); return(true); } }
public bool newProgress(string name, List <int> vector) { //判断是否超过resCount向量,若超过,返回false报错 List <int> resCountList = resCount.getDataList(); if (!BankerHelper.VectorLess(vector, resCountList)) { LogoutEventArgs e = new LogoutEventArgs("error: 新建进程的最大需求资源数超过最大资源数."); Logout.NewMsg(e); return(false); } if (max.newRow(name)) { //修改max max.modify(name, vector); need.newRow(name); allocation.newRow(name); //增加进程数 progNum++; //计算need矩阵的值 List <List <Int32> > t = new List <List <int> >(); for (int i = 0; i < progNum; i++) { t.Add(new List <int>()); for (int j = 0; j < resNum; j++) { t[i].Add(0); } } BankerHelper.MatrixRed(max.getData(), allocation.getData(), ref t); need.modify(t); return(true); } return(false); }
//银行家算法 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); }