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(); }
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; }
public void Remove(Banker value) { List.Remove(value); }
public void Insert(int index, Banker value) { List.Insert(index, value); }
public int IndexOf(Banker value) { return List.IndexOf(value); }
public bool Contains(Banker value) { // If value is not of type Int16, this will return false. return (List.Contains(value)); }
public int Add(Banker value) { return (List.Add(value)); }
/// <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; }