예제 #1
0
 public Object Clone()
 {
     BankerCollect newBC = new BankerCollect();
     for (int i = 0; i < this.Count; i++)
     {
         newBC.Add(this[i].Clone());
     }
     return newBC;
 }
예제 #2
0
 public BankerControl(int amount)
 {
     _bankerCollect = new BankerCollect();
     for (int i = 0; i < amount; i++)
     {
         Banker banker = new Banker();
         _bankerCollect.Add(banker);
     }
     _unDoneCount = _bankerCollect.Count;
     _waitQueue = new List<Process>();
     init();
 }
예제 #3
0
 /// <summary>
 /// use safe checking algorithm
 /// </summary>
 /// <param name="bc"></param>
 /// <returns></returns>
 private bool runCheck(BankerCollect bc, List<int> avaliable)
 {
     _isSafe = false;
     checkSafe(bc, avaliable);
     return _isSafe;
 }
예제 #4
0
        /// <summary>
        /// core safe check recursion algorithm
        /// </summary>
        /// <param name="bc"></param>
        private void checkSafe(BankerCollect bc, List<int> avaliable)
        {
            if (bc.Count == 0)
            {
                _isSafe = true;
                return;
            }
            BankerCollect tempB = (BankerCollect)bc.Clone();   // create a temp BankerCollect
            for (int i = 0; i < tempB.Count; i++)
            {
                Banker tempBank = tempB[i];
                List<int> newA = listClone(avaliable);
                if (_isSafe)                // if recursion calculate that the sequence is safe, then quit function
                {
                    return;
                }
                if (tempBank.IsDone)
                {
                    tempB.Remove(tempBank);    // then remove it and recurse
                    checkSafe(tempB, newA);
                    i--;
                }
                else
                {
                    bool isSatisfy = true;
                    for (int j = 0; j < Data.ResCount; j++)
                    {
                        if (tempBank.Need[j] > newA[j])
                        {
                            isSatisfy = false;
                            break;
                        }
                    }
                    if (isSatisfy)          // check if avaliable resources satisfy with one Banker's need
                    {
                        delAvailable(newA, tempBank.Need);
                        addAvailable(newA, tempBank.Claim);
                        tempB.Remove(tempBank);    // then remove it and recurse
                        checkSafe(tempB, newA);
                        i--;
                    }
                }

            }
        }
예제 #5
0
        /// <summary>
        /// request resource in random way
        /// </summary>
        /// <returns>true: allocate ; false: add into the waiting queue</returns>
        public bool Request()
        {
            if (_unDoneCount <= 0)
            {
                throw new Exception("All processes have been allocated and finished!!");
            }
            do
            {
                _proIndex = Data.Random.Next(_bankerCollect.Count);
            } while (_bankerCollect[ProIndex].IsDone);
            _currentBanker = _bankerCollect[_proIndex];
            bool isZero = true;
            do
            {
                isZero = true;
                _requestRes = new List<int>(Data.ResCount);
                for (int i = 0; i < Data.ResCount; i++) // make sure that request resources <= need resources
                {
                    int requireLimit = _currentBanker.Need[i];
                    int res = Data.Random.Next(requireLimit + 1);
                    _requestRes.Add(res);
                    if (isZero && res > 0)
                        isZero = false;
                }
            } while (isZero); // make sure that all kinds of request resources are not 0 at the same time

            List<int> newA = listClone(_avaliable);

            for (int i = 0; i < Data.ResCount; i++)
            {
                if (_requestRes[i] > newA[i])
                {
                   // _waitQueue.Add(new Process(_proIndex, _requestRes));
                    return false;
                }
            }
            BankerCollect tempB = (BankerCollect)_bankerCollect.Clone();
            tempB[_proIndex].allocate(_requestRes);
            delAvailable(newA, _requestRes);
            if (tempB[_proIndex].IsDone)
            {
                addAvailable(newA, tempB[_proIndex].Claim);
            }

            ////---------------------------------------------------
            //ShowConsoleAvaliable();
            //for (int i = 0; i < tempB.Count; i++)
            //{
            //    Console.Write("Process " + i + " : ");
            //    for (int j = 0; j < tempB[i].Need.Count; j++)
            //    {
            //        Console.Write(tempB[i].Need[j] + " ");
            //    }
            //    Console.WriteLine();
            //}

            if (!runCheck(tempB, newA))   // if no safe after allocating resources, then add request to waiting queue
            {
                //_waitQueue.Add(new Process(_proIndex, _requestRes));
                return false;
            }

            ////---------------------------------------------------
            //Console.WriteLine("safeer");
            //ShowConsoleAvaliable();
            //for (int i = 0; i < tempB.Count; i++)
            //{
            //    Console.Write("Process " + i + " : ");
            //    for (int j = 0; j < tempB[i].Need.Count; j++)
            //    {
            //        Console.Write(tempB[i].Need[j] + " ");
            //    }
            //    Console.WriteLine();
            //}

            if (tempB[_proIndex].IsDone)
            {
                _unDoneCount--;
            }
            _avaliable = newA;
            _bankerCollect = tempB;
            return true;
        }
예제 #6
0
        /// <summary>
        /// excute the requests in waiting queue
        /// </summary>
        /// <returns>if finish a request or not</returns>
        public bool ExcuteQueue()
        {
            if (_waitQueue.Count <= 0 || _unDoneCount <= 0)
                return false;
            for (int i = 0; i < _waitQueue.Count; i++)
            {
                BankerCollect tempB = (BankerCollect)_bankerCollect.Clone();
                List<int> newA = listClone(_avaliable);
                if (!tempB[_waitQueue[i].ProIndex].IsDone && tempB[_waitQueue[i].ProIndex].allocate(_waitQueue[i].RequestRes))
                {
                    delAvailable(newA, _waitQueue[i].RequestRes);
                    if (runCheck(tempB, newA))   // if no safe after allocating resources, then check next request in waiting queue
                    {
                        delAvailable(_avaliable, _waitQueue[i].RequestRes);

                        if (tempB[_waitQueue[i].ProIndex].IsDone)
                        {
                            addAvailable(_avaliable, tempB[_waitQueue[i].ProIndex].Claim);
                            _unDoneCount--;
                        }
                        _bankerCollect = tempB;
                        _requestRes = _waitQueue[i].RequestRes;
                        _proIndex = _waitQueue[i].ProIndex;
                        _waitQueue.Remove(_waitQueue[i]);
                        return true;
                    }
                }
                else   // if request in waiting queue > banker's need, then remove it.
                {
                    _waitQueue.Remove(_waitQueue[i]);
                    i--;
                }
            }
            return false;
        }