Exemplo n.º 1
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);
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }