Esempio n. 1
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();
 }
Esempio n. 2
0
 public Banker Clone()
 {
     Banker newBanker = new Banker();
     newBanker._allocation = new List<int>();
     newBanker._claim = new List<int>();
     newBanker._need = new List<int>();
     for (int i = 0; i < Data.ResCount; i++)
     {
         newBanker._allocation.Add(this._allocation[i]);
         newBanker._claim.Add(this._claim[i]);
         newBanker._need.Add(this._need[i]);
     }
     newBanker._isDone = this._isDone;
     return newBanker;
 }
Esempio n. 3
0
 public void Remove(Banker value)
 {
     List.Remove(value);
 }
Esempio n. 4
0
 public void Insert(int index, Banker value)
 {
     List.Insert(index, value);
 }
Esempio n. 5
0
 public int IndexOf(Banker value)
 {
     return List.IndexOf(value);
 }
Esempio n. 6
0
 public bool Contains(Banker value)
 {
     // If value is not of type Int16, this will return false.
     return (List.Contains(value));
 }
Esempio n. 7
0
 public int Add(Banker value)
 {
     return (List.Add(value));
 }
Esempio n. 8
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;
        }