private static int SortByArrivalTime(Process x, Process y) { if(x.ArrivalTime > y.ArrivalTime){ return 1; }else if(x.ArrivalTime == y.ArrivalTime){ return 0; }else{ return -1; } }
private static int SortByPriority(Process x, Process y) { if(x.Priority > y.Priority){ return 1; }else if(x.Priority == y.Priority){ return 0; }else{ return -1; } }
private static int SortByTimeWorkedOnQuantum(Process x, Process y) { if(x.timeWorkedOnQuantum > y.timeWorkedOnQuantum){ return 1; }else if(x.timeWorkedOnQuantum == y.timeWorkedOnQuantum){ return 0; }else{ return -1; } }
// inherited methods public void AddProcess(Process p) { _queues[0].AddProcess(p); }
// inherited methods public override void RemoveProcess(Process p) { throw new NotImplementedException (); }
//Add the finished process to the _finishedProcesses queue public static void AddFinishedProcess(Process finishedProcess) { lock(_locker){ _finishedProcesses.Add(finishedProcess); } }
public void StartSimulation() { // generate core objects Core newCore; for(int i = 0; i < _numCores; i++) { newCore = new Core(i, _queueTypes, _quantums); _coreList.Add( newCore ); } int cpuBurstTime; int ioBurstTime; int pid; int priority; int arrivalTime = 0; int lastArrivalTime; string processState = "Waiting"; Random random = new Random(); Process p; //Reference to process objects created below //Generate processes for(int i = 0; i <_numProcesses; i++) { cpuBurstTime = random.Next( _minCpuBurstTimeRemaining, _maxCpuBurstTimeRemaining ); ioBurstTime = random.Next( _minIOBurstTimeRemaining, _maxIOBurstTimeRemaining ); pid = i; priority = random.Next(0,9); lastArrivalTime = arrivalTime; if ( i != 0 ) // make sure the first arrival time is at 0 arrivalTime = lastArrivalTime + random.Next(0, _arrivalTimeRange); p = new Process(pid, priority, arrivalTime, processState, cpuBurstTime, ioBurstTime); _generatedProcesses.Enqueue(p); // add the process to the list of generated processes } // generate core threads for(int i = 0; i < _numCores; i++){ _coreThreadList.Add(new Thread(new ThreadStart(_coreList[i].DoWork))); } int result = 0; // run the core threads until finished try { //Start all of the threads for(int i = 0; i < _numCores; i++){ _coreThreadList[i].Start (); } //Increment the System time, and perform load balancing while(!stopProcessing){ //While the cores have work to do, OR, more processes are to be created LoadNewlyArrivedProcesses(); //Check all of the cores, if all of them are complete, set stopProcessing to true; //stopProcessing = isFinishedProcessing(); if(isFinishedProcessing() && (_generatedProcesses.Count == 0)){ stopProcessing = true; } //Unblock all of the Core Threads, allowing them to execute manualEvent2.Reset (); manualEvent.Set (); if(stopProcessing){ break; } //Wait for all of the Core Threads to execute(Number of handles cannot be greater than 64!) WaitHandle.WaitAll(autoEvents); //Block all of the Core Threads, preventing them from executing manualEvent.Reset (); manualEvent2.Set (); //Print out the messages generated by the Cores GlobalVar.PrintMessages(); //For some reason, a sleep is needed to prevent the program from hanging. Thread.Sleep (10); //Increment the systemTime variable systemTime++; //Reset the status of all of the Cores SetUnFinished(); } //Join all of the threads for(int i = 0; i < _numCores; i++){ _coreThreadList[i].Join (); } //At this point all of the threads are complete } catch (ThreadStateException e) { Console.WriteLine(e); // Display text of exception result = 1; // Result says there was an error } catch (ThreadInterruptedException e) { Console.WriteLine(e); // This exception means that the thread // was interrupted during a Wait result = 1; // Result says there was an error } Environment.ExitCode = result; //Notify user that processing is complete GlobalVar.OutputMessage("****Processor Finished****" + Environment.NewLine); //Display the turnaround time, response time, wait time throughputTime = _finishedProcesses[ _finishedProcesses.Count - 1 ].CompletionTime / _finishedProcesses.Count; GlobalVar.OutputMessage("Throughput Time is " + throughputTime.ToString() + " clock cycles."); turnaroundTime = 0; for(int i = 0; i < _finishedProcesses.Count; i++){ turnaroundTime += (_finishedProcesses[i].CompletionTime - _finishedProcesses[i].ArrivalTime); } turnaroundTime /= _finishedProcesses.Count; GlobalVar.OutputMessage("Turnaround Time is " + turnaroundTime.ToString() + " clock cycles."); waitTime = 0; for(int i = 0; i < _finishedProcesses.Count; i++){ waitTime += (_finishedProcesses[i].CompletionTime-_finishedProcesses[i].ArrivalTime - _finishedProcesses[i].CpuBurstTime - _finishedProcesses[i].totalContextSwitchCosts); } waitTime /= _finishedProcesses.Count; GlobalVar.OutputMessage("Wait Time is " + waitTime.ToString() + " clock cycles."); responseTime = 0; for(int i = 0; i < _finishedProcesses.Count; i++){ responseTime += (_finishedProcesses[i].responseTime-_finishedProcesses[i].ArrivalTime); } responseTime /= _finishedProcesses.Count; GlobalVar.OutputMessage("Response Time is " + responseTime.ToString() + " clock cycles."); contextSwitchTime = 0; for(int i = 0; i < _finishedProcesses.Count; i++){ contextSwitchTime += (_finishedProcesses[i].totalContextSwitchCosts); } contextSwitchTime /= _finishedProcesses.Count; GlobalVar.OutputMessage("Context Switch Time is " + contextSwitchTime.ToString() + " clock cycles."); GlobalVar.PrintMessages(); }
// Methods public override void RemoveProcess(Process p) { }
public abstract void RemoveProcess(Process p);
public void AddProcess(Process p) { _processesQ.Add(p); }