示例#1
0
        //修改资源数
        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);
        }
示例#2
0
 private bool isOverFlow(List <int> newRes)
 {
     for (int i = 0; i < progNum; i++)
     {
         if (!BankerHelper.VectorLess(max.getVector(i), newRes))
         {
             return(true);
         }
     }
     return(false);
 }
示例#3
0
 private int existProperProgress(List <bool> finish, List <int> work)
 {
     for (int i = 0; i < progNum; i++)
     {
         if (finish[i] == false && BankerHelper.VectorLess(need.getVector(i), work))
         {
             return(i);
         }
     }
     return(-1);
 }
示例#4
0
        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);
        }
示例#5
0
        //安全性算法,返回的是安全序列,当无安全序列时返回null
        private List <string> safetyAlg(List <int> request)
        {
            //步骤1
            List <int>  work   = new List <int>(available.getDataList().ToArray());
            List <bool> finish = new List <bool>();

            for (int i = 0; i < progNum; i++)
            {
                finish.Add(false);
            }
            List <string> safetyList = new List <string>();//安全序列
            int           t          = existProperProgress(finish, work);

            while (t != -1)
            {
                string name = max.getProgressNameList()[t];
                //步骤3
                for (int j = 0; j < resNum; j++)
                {
                    work[j] += allocation[t, j];
                }
                finish[t] = true;
                safetyList.Add(name);
                workAllocation.newRow(name);
                workAllocation.modify(name, work);
                t = existProperProgress(finish, work);
            }
            //步骤4
            if (BankerHelper.isAllFinish(finish))
            {
                return(safetyList);
            }
            else
            {
                return(null);
            }
        }
示例#6
0
        public void newRes()
        {
            resNum++;
            resCount.newColumn();
            available.newColumn();
            max.newColumn();
            allocation.newColumn();
            need.newColumn();
            workAllocation.newColumn();

            //计算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);
        }
示例#7
0
        //银行家算法
        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);
        }