diff --git a/Assets/Scenes/Scheduler.unity b/Assets/Scenes/Scheduler.unity index 408210c..3a6d32e 100644 --- a/Assets/Scenes/Scheduler.unity +++ b/Assets/Scenes/Scheduler.unity @@ -769,6 +769,7 @@ GameObject: - component: {fileID: 96677239} - component: {fileID: 96677241} - component: {fileID: 96677242} + - component: {fileID: 96677243} m_Layer: 5 m_Name: Data Scheduler m_TagString: Untagged @@ -820,6 +821,7 @@ MonoBehaviour: summaryManager: {fileID: 1754978061} fcfsScheduler: {fileID: 96677239} sjfScheduler: {fileID: 96677242} + sjfPreemptiveScheduler: {fileID: 96677243} SchedulerTime: 0 running: 0 ProcessList: [] @@ -853,6 +855,20 @@ MonoBehaviour: m_EditorClassIdentifier: scheduler: {fileID: 96677240} chartMaker: {fileID: 96677241} +--- !u!114 &96677243 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 96677237} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fed2c40f90adca343a3fd328f7f3a985, type: 3} + m_Name: + m_EditorClassIdentifier: + scheduler: {fileID: 96677240} + chartMaker: {fileID: 96677241} --- !u!1 &182682647 GameObject: m_ObjectHideFlags: 0 @@ -5734,7 +5750,7 @@ MonoBehaviour: m_HandleRect: {fileID: 969577460} m_Direction: 0 m_Value: 0 - m_Size: 0.99998224 + m_Size: 0.99999845 m_NumberOfSteps: 0 m_OnValueChanged: m_PersistentCalls: @@ -10343,7 +10359,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: 0.00061035156, y: 0} + m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0.5, y: 1} --- !u!114 &1518418616 diff --git a/Assets/src/PropertiesData.cs b/Assets/src/PropertiesData.cs index 64d98c8..69f83c1 100644 --- a/Assets/src/PropertiesData.cs +++ b/Assets/src/PropertiesData.cs @@ -6,7 +6,7 @@ public class PropertiesData : MonoBehaviour public int ArrivalTime = 0; public int BurstTime = 1; public int Priority = 0; - + public float remainingBurstTime = 0; public void UpdateProcessName(string _ProcessName) { ProcessName = _ProcessName; @@ -24,6 +24,7 @@ public void UpdateBurstTime(string _BurstTime) try { BurstTime = int.Parse(_BurstTime); + remainingBurstTime = BurstTime; } catch { } } diff --git a/Assets/src/SJFPreemptiveScheduler.cs b/Assets/src/SJFPreemptiveScheduler.cs new file mode 100644 index 0000000..878beee --- /dev/null +++ b/Assets/src/SJFPreemptiveScheduler.cs @@ -0,0 +1,84 @@ +using System.Collections.Generic; +using UnityEngine; + +public class SJFPreemptiveScheduler : MonoBehaviour +{ + public Scheduler scheduler; + public ChartMaker chartMaker; + private List arrived; + private List waiting; + bool running = false; + bool processing = false; + PropertiesData CurrentlyProcessing; + private float ProcessorFreeAt = 0.0f; + private float ProcessStartedAt = 0.0f; + public void run() + { + waiting = new List(); + arrived = new List(); + foreach (var _process in scheduler.ProcessList) + { + waiting.Add(_process); + } + running = true; + } + void Update() + { + if (running) + { + for (int i = 0; i < waiting.Count; i++) + { + PropertiesData propertiesData = waiting[i]; + if (scheduler.SchedulerTime >= propertiesData.ArrivalTime) + { + arrived.Add(propertiesData); + waiting.Remove(propertiesData); + } + } + if (!processing) + { + if (arrived.Count > 0) + { + float minBurst = arrived[0].BurstTime; + CurrentlyProcessing = arrived[0]; + for (int i = 0; i < arrived.Count; i++) + { + PropertiesData processes = arrived[i]; + if (processes.BurstTime < minBurst) + { + minBurst = arrived[i].BurstTime; + CurrentlyProcessing = arrived[i]; + } + } + arrived.Remove(CurrentlyProcessing); + processing = true; + ProcessorFreeAt = Mathf.Min(CurrentlyProcessing.BurstTime, 1); + ProcessStartedAt = scheduler.SchedulerTime; + chartMaker.GenerateChartElement(CurrentlyProcessing.ProcessName, scheduler.SchedulerTime); + } + } + else + { + ProcessorFreeAt -= Time.deltaTime * Time.timeScale; + if (ProcessorFreeAt <= 0) + { + if (CurrentlyProcessing.remainingBurstTime <= 0) + { + scheduler.makeSummary(CurrentlyProcessing); + } + else + { + CurrentlyProcessing.remainingBurstTime -= (scheduler.SchedulerTime - ProcessStartedAt); //1 + arrived.Add(CurrentlyProcessing); + } + processing = false; + if (waiting.Count == 0 && arrived.Count == 0) + { + running = false; + scheduler.running = false; + } + } + } + } + } +} diff --git a/Assets/src/SJFPreemptiveScheduler.cs.meta b/Assets/src/SJFPreemptiveScheduler.cs.meta new file mode 100644 index 0000000..c4c8051 --- /dev/null +++ b/Assets/src/SJFPreemptiveScheduler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fed2c40f90adca343a3fd328f7f3a985 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/src/SJFScheduler.cs b/Assets/src/SJFScheduler.cs index 48f7e8a..1c07a21 100644 --- a/Assets/src/SJFScheduler.cs +++ b/Assets/src/SJFScheduler.cs @@ -1,5 +1,4 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; public class SJFScheduler : MonoBehaviour diff --git a/Assets/src/Scheduler.cs b/Assets/src/Scheduler.cs index db89736..fbd7bea 100644 --- a/Assets/src/Scheduler.cs +++ b/Assets/src/Scheduler.cs @@ -7,6 +7,7 @@ public class Scheduler : MonoBehaviour public SummaryManager summaryManager; public FCFSScheduler fcfsScheduler; public SJFScheduler sjfScheduler; + public SJFPreemptiveScheduler sjfPreemptiveScheduler; [HideInInspector] public float SchedulerTime; [HideInInspector] public bool running = false; [HideInInspector] public List ProcessList; @@ -24,7 +25,7 @@ public void RunScheduler() { if (tabData.preemptive) { - + sjfPreemptiveScheduler.run(); } else {