Created
March 7, 2017 11:22
-
-
Save jeremysinger/c9f31d23e11bb59742f1051caf14d3b6 to your computer and use it in GitHub Desktop.
shortest job first example code
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
package os.coursework; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
/** | |
* Shortest Job First (SJF) scheduling model | |
* reads in a set of processes from CSV file, | |
* one process per line in format ^ID, CBT, AAT$ | |
* and computes various metrics including: | |
* - mean wait time | |
* - mean turnaround time | |
* | |
* Note that this is a non-preemptive SJF | |
* implementation, so: | |
* turnaround time == wait time + CPU burst time | |
*/ | |
public class SJF { | |
public static String inputFileName; | |
public static void main(String [] args) { | |
if (args.length != 1) { | |
System.err.println("usage: SJF FILENAME.csv"); | |
System.exit(-1); | |
} | |
inputFileName = args[0]; | |
int currentTime = 0; | |
int totalTimeCPUBusy = 0; | |
int totalWaitingTime = 0; | |
int totalTurnaroundTime = 0; | |
int numTasks = 0; | |
ArrayList<Process> l = new ArrayList<>(); | |
try (BufferedReader r = new BufferedReader(new FileReader(inputFileName))) { | |
String line = r.readLine(); | |
while (line != null) { | |
// each line has 3 comma separated values | |
String [] data = line.split(","); | |
assert (data.length == 3); | |
numTasks++; | |
Process p = new Process( | |
Integer.parseInt(data[0].trim()), | |
Integer.parseInt(data[1].trim()), | |
Integer.parseInt(data[2].trim())); | |
// put process in list | |
l.add(p); | |
line = r.readLine(); | |
} | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
assert (l.size() == numTasks); | |
Collections.sort(l); | |
// time to do simulation of scheduling | |
while (l.size() > 0) { | |
// pseudo-code | |
// iterate over l, look for first task that is schedulable | |
Process firstProcess = null; | |
boolean processWasScheduled = false; | |
for (Process p : l) { | |
if (firstProcess == null || | |
firstProcess.aat > p.aat) { | |
firstProcess = p; | |
} | |
if (p.aat <= currentTime) { | |
int waitingTime, turnaroundTime; | |
waitingTime = (currentTime - p.aat); | |
totalWaitingTime += waitingTime; | |
// schedule p | |
l.remove(p); | |
// add its CBT to totalExecutionTime | |
totalTimeCPUBusy+= p.cbt; | |
currentTime += p.cbt; | |
processWasScheduled = true; | |
turnaroundTime = (currentTime - p.aat); | |
// == (waitingTime + p.cbt) | |
totalTurnaroundTime += turnaroundTime; | |
System.out.printf("PID %d, WT %d TT %d\n", p.id, waitingTime, turnaroundTime); | |
break; | |
} | |
if (!processWasScheduled) { | |
assert (firstProcess != null); | |
// fast-forward currentTime | |
// since there are no processes ready | |
// to execute | |
currentTime = firstProcess.aat; | |
} | |
} | |
} | |
System.out.println("average waiting time (AWT): " + | |
totalWaitingTime / numTasks); | |
System.out.println("average turnaround time (ATT): " + | |
totalTurnaroundTime / numTasks); | |
} | |
} /* SJF */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment