/// <summary> /// This is where the algorithm works (does its job) /// </summary> public override void Work() { if (!ready) { return; } PCB shortest = null; double minLength = int.MaxValue; foreach (var pcb in Pool) { //calculate Response Ratio if (pcb.State != PCB.ProcessState.WAITING && pcb.State != PCB.ProcessState.DEAD && pcb.Process.Length < minLength) { //set shortest shortest = pcb; //set min burst time minLength = pcb.Process.Length; } } if (current != shortest) { current = shortest; } }
/// <summary> /// This is where the algorithm works (does its job) /// </summary> public override void Work() { if (!ready) { return; } PCB shortest = null; double maxRespRatio = 0; foreach (var pcb in Pool) { //calculate Response Ratio if (pcb.State != PCB.ProcessState.WAITING && pcb.State != PCB.ProcessState.DEAD && (pcb.Process.WaitTime + pcb.Process.RemainingTick) / pcb.Process.RemainingTick > maxRespRatio) { //set shortest shortest = pcb; //set max burst time maxRespRatio = (pcb.Process.RemainingTick + pcb.Process.Progress) / pcb.Process.RemainingTick; } } if (current != shortest) { current = shortest; } }
/// <summary> /// Constructor of the Control /// </summary> /// <param name="pcb"></param> public ProcessSimulationRow(PCB pcb) { InitializeComponent(); labelPID.Text = pcb.PID.ToString(); labelName.Text = pcb.Process.Name; labelState.Text = pcb.State.ToString(); ChangeBackgroundByState(pcb.State); }
/// <summary> /// Removes the current process from the CPU /// </summary> public void UnsetProcess() { if (currentProcess != null) { currentProcess = null; working = false; } }
/// <summary> /// Encapsulate a single process into PCB (Process Control Block) /// </summary> /// <param name="process">The process</param> /// <returns>The encapsulated PCB</returns> public PCB AddAndEncapsulateProcess(Process process) { EventLogger.AddEvent("New process: " + process.ToString()); PCB pcb = new PCB(counter++, process); ProcessList.Add(pcb); //Add process to the scheduling algorithm too selectedAlgorithm.AddNewProcess(pcb); return(pcb); }
private void UpdateUI() { // Update label async labelUtil.BeginInvoke((Action)(() => { labelUtil.Text = running ? (Math.Round(scheduler.UsefulCPUTime * 100).ToString() + "%") : "-"; })); labelTurnaround.BeginInvoke((Action)(() => { labelTurnaround.Text = scheduler.Turns.ToString(); })); labelCurrTime.BeginInvoke((Action)(() => { labelCurrTime.Text = scheduler.ElapsedTime.ToString(); })); int fullLength = 0; int curr = 0; foreach (var pcb in scheduler.ProcessList) { fullLength += pcb.Process.Length; curr += pcb.Process.Progress; } labelCurrTime.BeginInvoke((Action)(() => { labelCompletion.Text = Math.Round((double)curr * 100 / fullLength).ToString() + "%"; })); // Sets dynamic parameters of processes in the list // Update process view foreach (KeyValuePair <int, ProcessSimulationRow> elem in processes) { PCB pcb = scheduler.GetProcessByID(elem.Key); elem.Value.UpdateData(pcb); } // Updates the Ready Queue label string readyQueueString = ""; foreach (PCB process in scheduler.ReadyProcesses) { readyQueueString += "P#"; readyQueueString += process.PID.ToString(); readyQueueString += " "; } labelrQueue.BeginInvoke((Action)(() => { labelrQueue.Text = readyQueueString; })); }
/// <summary> /// Adds process to the Process View Panel /// </summary> /// <param name="pcb">The pcb</param> private void AddProcessToView(PCB pcb) { ProcessSimulationRow psr = new ProcessSimulationRow(pcb) { Height = 30, //FlowLayout is tricky //Element width is determined by an element which has a width explicitly //In our case this is the Header or 'table' Anchor = (AnchorStyles.Left | AnchorStyles.Right) }; processes.Add(pcb.PID, psr); processViewPanel.Controls.Add(psr); }
/// <summary> /// As the method name says, it does work (everything) /// </summary> /// <returns>Is CPU currently working</returns> public bool DoWork() { if (currentProcess != null) { working = currentProcess.Work(); if (currentProcess.State == PCB.ProcessState.DEAD) { currentProcess = null; } return(working); } return(false); }
/// <summary> /// This is where the algorithm works (does its job) /// </summary> public override void Work() { if (!ready) { return; } PCB shortest = Pool.Where(x => x.State != PCB.ProcessState.DEAD && x.State != PCB.ProcessState.WAITING) .OrderBy(x => x.Process.Length - x.Process.Progress).FirstOrDefault(); if (current != shortest) { current = shortest; } }
/// <summary> /// Changes layout by a specified process /// </summary> /// <param name="pcb">Process which the layout should be based on</param> public void UpdateData(PCB pcb) { labelState.Text = pcb.State.ToString(); progressBarProgress.Maximum = pcb.Process.Length; progressBarProgress.Value = pcb.Process.Progress; if (pcb.Process.CurrentIO != null) { progressBarIO.Maximum = pcb.Process.CurrentIO.Length; progressBarIO.Value = pcb.Process.CurrentIO.Progress; } else { progressBarIO.Value = 0; } ChangeBackgroundByState(pcb.State); }
/// <summary> /// Sets the current process /// </summary> /// <param name="process">Process which is dhould be processed by the CPU</param> public void SetProcess(PCB process) { currentProcess = process; working = true; }
/// <summary> /// Constructor of the CPU Simulator /// </summary> public CPU() { working = false; currentProcess = null; }
/// <summary> /// Clears the algorithm, resets it to defaults /// </summary> public virtual void Reset() { pool.Clear(); current = null; ready = false; }
/// <summary> /// Adds new processes (even at runtime) /// </summary> /// <param name="pcb"></param> public void AddNewProcess(PCB pcb) { pool.Add(pcb); }