Skip to content

Commit

Permalink
SJF Preemptive Scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyanshanchlia committed Mar 17, 2020
1 parent 01a26a8 commit 320fa58
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 6 deletions.
20 changes: 18 additions & 2 deletions Assets/Scenes/Scheduler.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -820,6 +821,7 @@ MonoBehaviour:
summaryManager: {fileID: 1754978061}
fcfsScheduler: {fileID: 96677239}
sjfScheduler: {fileID: 96677242}
sjfPreemptiveScheduler: {fileID: 96677243}
SchedulerTime: 0
running: 0
ProcessList: []
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Assets/src/PropertiesData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +24,7 @@ public void UpdateBurstTime(string _BurstTime)
try
{
BurstTime = int.Parse(_BurstTime);
remainingBurstTime = BurstTime;
}
catch { }
}
Expand Down
84 changes: 84 additions & 0 deletions Assets/src/SJFPreemptiveScheduler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections.Generic;
using UnityEngine;

public class SJFPreemptiveScheduler : MonoBehaviour
{
public Scheduler scheduler;
public ChartMaker chartMaker;
private List<PropertiesData> arrived;
private List<PropertiesData> 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<PropertiesData>();
arrived = new List<PropertiesData>();
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;
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/src/SJFPreemptiveScheduler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Assets/src/SJFScheduler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;

public class SJFScheduler : MonoBehaviour
Expand Down
3 changes: 2 additions & 1 deletion Assets/src/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PropertiesData> ProcessList;
Expand All @@ -24,7 +25,7 @@ public void RunScheduler()
{
if (tabData.preemptive)
{

sjfPreemptiveScheduler.run();
}
else
{
Expand Down

0 comments on commit 320fa58

Please sign in to comment.