-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01a26a8
commit 320fa58
Showing
6 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters