static void arrange(process task, float total) { task.on_time = task.burstTime; task.completionTime = total - 0; task.turnArroundTime = task.completionTime - task.arrivalTime; task.waitingTime = task.turnArroundTime - task.burstTime; output.Add(task); }
private void SupportListProcess() { arrival_time_int = Array.ConvertAll(arrivalBox.Text.Split(' '), float.Parse); duration_int = Array.ConvertAll(burstBox.Text.Split(' '), float.Parse); if (process_type == "Priority (Preemptive)" || process_type == "Priority (Non Preemptive)") { priority_int = Array.ConvertAll(priorityBox.Text.Split(' '), int.Parse); } else if (process_type == "Round Robin") { quantumTime = int.Parse(quantemBox.Text); } process processi = new process(); for (int i = 0; i < number_processes; i++) { processi.pID = i + 1; processi.arrivalTime = arrival_time_int[i]; processi.started = 0; processi.on_time = 1; processi.end = 0; processi.burstTime = duration_int[i]; processi.labcolor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)); if (process_type == "Priority (Preemptive)" || process_type == "Priority (Non Preemptive)") { processi.priority = priority_int[i]; } burstTime[i] = duration_int[i]; processes.Add(processi); //Console.WriteLine(processi.arrivalTime); } // quantumTime = quantum; processes.Sort((x, y) => (x.arrivalTime.CompareTo(y.arrivalTime))); }
// Priority & SJF static void priority_sjf() { process p = new process(); float d; // detect the current timing of the process // var k = list.First; var k = processes[0]; d = k.arrivalTime; RQ(k); processes.RemoveAt(0); if (processes.Count != 0) { k = processes[0]; } else { k.end = 1; } float turnAroundTime = 0; averageWaitingTime = 0; while (queue.Count != 0 || processes.Count != 0) { if (k.end != 1 && k.arrivalTime == d) { RQ(k); processes.RemoveAt(0); if (processes.Count == 0) { k.end = 1; } else { k = processes[0]; } } else if (queue.Count == 0) { d = k.arrivalTime; } else { //Console.Write(queue.First.Value.pID + " "); p = queue.First.Value; queue.RemoveFirst(); //p.started++; //d++; //d = k.arrivalTime; if(k.end !=1 &&(k.arrivalTime - d) < (p.burstTime - p.started)) { p.started += (k.arrivalTime - d); p.on_time = (k.arrivalTime - d); d = k.arrivalTime; } else { float ll = p.burstTime - p.started; p.started += ll; p.on_time = ll; d += ll; } if (p.started == p.burstTime) { turnAroundTime = (d - p.arrivalTime); averageWaitingTime = averageWaitingTime + (turnAroundTime - p.burstTime) / (float)number_processes; } else { queue.AddFirst(p); } if (output.Count != 0 && output[output.Count - 1].pID == p.pID) { process m = output[output.Count - 1]; m.on_time+= p.on_time; output.RemoveAt(output.Count - 1); output.Add(m); } else { output.Add(p); } } } }
// Round Robin static void Round_R() { process p = new process(); float d; // detect the current timing of the process //float averageWaitingTime = 0; var k = processes[0]; d = k.arrivalTime; queue.AddFirst(k); processes.RemoveAt(0); if (processes.Count != 0) { k = processes[0]; } else { k.end = 1; } float turnAroundTime = 0; var temp = queue.First; bool detectProcessRemoval = false; bool smallerProcess = false; float FirstProcessArrivalTime = k.arrivalTime; while (queue.Count != 0 || processes.Count != 0) { if (k.end != 1 && queue.Count == 0) { float TempVar = k.arrivalTime - d; d += TempVar; queue.AddFirst(k); FirstProcessArrivalTime = k.arrivalTime; processes.RemoveAt(0); // remove that process from the sorted list if (processes.Count == 0) // if the list is empty { k.end = 1; // put 1 in that "detector variable" } else { k = processes[0]; // k -> list.next } } if ((k.end != 1 && k.arrivalTime <= d) && d != FirstProcessArrivalTime) { if (detectProcessRemoval) { queue.AddLast(k); processes.RemoveAt(0); if (processes.Count == 0) { k.end = 1; } else { k = processes[0]; } detectProcessRemoval = false; while (k.end != 1 && k.arrivalTime <= d) { processes.RemoveAt(0); queue.AddLast(k); if (processes.Count == 0) { k.end = 1; } else { k = processes[0]; } } } else { temp = queue.Last; queue.AddBefore(temp, k); processes.RemoveAt(0); if (processes.Count == 0) { k.end = 1; } else { k = processes[0]; } //list.RemoveFirst(); } } else if (queue.Count == 0) { d++; } else { process m = queue.First.Value; if (m.burstTime > quantumTime) { m.on_time = quantumTime; } else { m.on_time = m.burstTime; } output.Add(m); /* if (output.Count != 0 && output[output.Count - 1].pID == queue.First.Value.pID) { process m = output[output.Count - 1]; m.on_time+= quantumTime - 1; output.RemoveAt(output.Count - 1); output.Add(m); } else { output.Add(queue.First.Value); } */ smallerProcess = false; p = queue.First.Value; if (p.burstTime > quantumTime) { p.burstTime -= quantumTime; queue.RemoveFirst(); queue.AddLast(p); } else { smallerProcess = true; d += p.burstTime; p.burstTime = 0; turnAroundTime = (d - p.arrivalTime); averageWaitingTime = averageWaitingTime + (turnAroundTime - burstTime[queue.First.Value.pID - 1]) / (number_processes); queue.RemoveFirst(); detectProcessRemoval = true; } if (!smallerProcess) { d += quantumTime; } } } }
static void RQ(process p) { if (process_type == "SJF (Preemptive)" || process_type == "SJF (Non Preemptive)") { if (queue.Count == 0) { queue.AddFirst(p); } else if (queue.Count == 1) { if ((preemptive || queue.First.Value.started == 0) && p.burstTime < (queue.First.Value.burstTime - queue.First.Value.started)) { queue.AddFirst(p); } else queue.AddLast(p); } else if (p.burstTime >= (queue.Last.Value.burstTime - queue.Last.Value.started)) { queue.AddLast(p); } else { LinkedListNode<process> i = preemptive ? queue.First : queue.First.Next; while ((i.Value.burstTime- i.Value.started) <= p.burstTime) { i = i.Next; } queue.AddBefore(i, p); } } else if (process_type == "Priority (Preemptive)" || process_type == "Priority (Non Preemptive)") { if (queue.Count == 0) { queue.AddFirst(p); } else if (queue.Count == 1) { if ((preemptive || queue.First.Value.started == 0) && p.priority < queue.First.Value.priority) { queue.AddFirst(p); } else queue.AddLast(p); } else if (p.priority > queue.Last.Value.priority) { queue.AddLast(p); } else { LinkedListNode<process> i = preemptive ? queue.First : queue.First.Next; while (i.Value.priority <= p.priority) { i = i.Next; } queue.AddBefore(i, p); } } }