diff --git a/Scheduler.java b/A1.java similarity index 89% rename from Scheduler.java rename to A1.java index 5a3084a..af50144 100644 --- a/Scheduler.java +++ b/A1.java @@ -1,7 +1,7 @@ import java.util.*; import java.io.*; -public class Scheduler { +public class A1 { //Address of File name relative to code. Replace with request for filename? String fileName = "datafiles/datafile2.txt"; @@ -16,7 +16,7 @@ public class Scheduler { * Main. Runs the program */ public static void main(String[] args){ - Scheduler system = new Scheduler(); + A1 system = new A1(); system.run(); } @@ -28,16 +28,17 @@ public class Scheduler { readFile(fileName); //Read the file //printFile(); - FCFS fcfs = new FCFS(disp, processList); + Algorithm fcfs = new Algorithm(disp, processList, "FCFS"); fcfs.runDispatch(); - fcfs.printResults(); + printResults(); reset(); System.out.println(); - SRT srt = new SRT(disp, processList); + Algorithm srt = new Algorithm(disp, processList, "SRT"); srt.runDispatch(); - srt.printResults(); + printResults(); + reset(); } @@ -48,6 +49,17 @@ public class Scheduler { processList.get(i).reset(); } } + + public void printResults() + { + System.out.println("\nProcess \tTurnaround Time \tWaiting Time"); + for (int i = 0; i < processList.size(); i++) + { + System.out.print(processList.get(i).getId()+"\t\t"); + System.out.print(processList.get(i).getTurnTime()+"\t\t\t"); + System.out.print(processList.get(i).getWaitTime()+"\n"); + } + } /** * readFile. Reads the file given, creating processes as needed to save details. * @param fileNameInput address of the file to be read in. diff --git a/FCFS.java b/Algorithm.java similarity index 66% rename from FCFS.java rename to Algorithm.java index 8ca8937..16e5a0f 100644 --- a/FCFS.java +++ b/Algorithm.java @@ -1,23 +1,22 @@ import java.util.LinkedList; -/** - * Shortest Remaining Time - */ -public class FCFS { +public class Algorithm { private int timeStep = 0; private int disp; private int timeLastChecked = -1; + private String type; private static LinkedList processListFromFile; //Original List of processes from the the file private LinkedList waitingQueue = new LinkedList<>(); //Processes officially added to the waiting queue. private LinkedList newProcesses = new LinkedList<>(); //Processes that have arrived since the last check - //Constructors - public FCFS(int dispInput, LinkedList processListInput) + //Constructor + public Algorithm(int dispInput, LinkedList processListInput, String typeOfAlgorithm) { disp = dispInput; processListFromFile = processListInput; + type = typeOfAlgorithm; } //Check for new processes that have arrived @@ -64,7 +63,7 @@ public class FCFS { //Run the actual dispatch and running of programs public void runDispatch() { - System.out.println("FCFS:"); //Currently using FCFS methodology + System.out.println(type+":"); //Currently using FCFS methodology int processesFinished = 0; //Used to track if we've done all the processes from the file @@ -78,8 +77,19 @@ public class FCFS { //As long as there is something in the queue if(waitingQueue.size()>0) { - - currentProcess = findNextProcess(); + //Which process runs next depends on the algorithm used. + switch (type) { + case "FCFS": + currentProcess = waitingQueue.getFirst(); + break; + case "SRT": + currentProcess = findShortestProcess(); + break; + default: + currentProcess = waitingQueue.getFirst(); + break; + } + //If the next process to run is different than the last one run, we need to run the actual dispatcher, which takes time, to change process if(!lastRunProcess.getId().equals(currentProcess.getId())) @@ -94,9 +104,11 @@ public class FCFS { waitingQueue.get(i).wait(disp); } } - - runProcessTick(currentProcess); + //Move the simulation one step forward + progressOneTick(currentProcess); + + //If a process finishes, save details and remove it from the queue if(currentProcess.isDone()) { currentProcess.saveEndTime(timeStep); @@ -111,36 +123,44 @@ public class FCFS { }while(processesFinished!=processListFromFile.size()); } - private Process findNextProcess() + /** + * Finds and returns the process with the shortest remaining time till completion + * @return the process with the shortest remaining time till completion + */ + private Process findShortestProcess() { - // int listPos = -1; - // int remaining = waitingQueue.getFirst().getRemainingTime(); + int listPos = -1; + int remaining = waitingQueue.getFirst().getRemainingTime(); - // for(int i = 0; i processListFromFile; //Original List of processes from the the file - private LinkedList waitingQueue = new LinkedList<>(); //Processes officially added to the waiting queue. - private LinkedList newProcesses = new LinkedList<>(); //Processes that have arrived after each time point. - //Constructors - - public SRT(int dispInput, LinkedList processListInput) - { - disp = dispInput; - processListFromFile = processListInput; - } - - - private void checkForArrivals() - { - for(int i = 0; i< processListFromFile.size(); i++) - { - if((processListFromFile.get(i).getArrive()<=timeStep) && (processListFromFile.get(i).getArrive() > timeLastChecked)) - { - newProcesses.add(processListFromFile.get(i)); - } - } - - timeLastChecked = timeStep; - - //do any tie breaking by sorting the new processes based on name - boolean swap = false; - do - { - swap = false; - for(int i = 0; i< newProcesses.size()-1; i++) - { - if(Integer.parseInt(newProcesses.get(i).getId().substring(1))>Integer.parseInt(newProcesses.get(i+1).getId().substring(1))) - { - Process temp = newProcesses.get(i); - newProcesses.remove(i); - newProcesses.add(i+1, temp); - swap = true; - } - } - }while(swap); - - //Move the newly arrived (and organised) processes to the queue. - for(int i =0; i < newProcesses.size(); i++) - { - waitingQueue.add(newProcesses.get(i)); - } - newProcesses.clear(); - } - - public void runDispatch() - { - System.out.println("SRT:"); - int processesFinished = 0; - Process currentProcess; - Process lastRunProcess = new Process("temp", 0, 0, 0); //temp process for comparing the first run - - do - { - checkForArrivals(); //Check for any new processes since timestep - - //As long as there is something in the queue - if(waitingQueue.size()>0) - { - currentProcess = findShortestProcess(); - - if(!lastRunProcess.getId().equals(currentProcess.getId())) - { - timeStep = timeStep+disp; - System.out.println("T"+timeStep+": "+currentProcess.getId()); - for(int i = 0; i