import java.util.LinkedList; 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 //Constructor public Algorithm(int dispInput, LinkedList processListInput, String typeOfAlgorithm) { disp = dispInput; processListFromFile = processListInput; type = typeOfAlgorithm; } //Check for new processes that have arrived private void checkForArrivals() { //Check each processe from the file, and if it has arrived between timeStep and whenever we last checked, add it. 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)); } //Clear the processes list so it is ready to go for the next time newProcesses.clear(); } //Run the actual dispatch and running of programs public void runDispatch() { System.out.println(type+":"); //Currently using FCFS methodology int processesFinished = 0; //Used to track if we've done all the processes from the file 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) { //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())) { timeStep += disp; System.out.println("T"+timeStep+": "+currentProcess.getId()); //Add the time waited to each of the currently waiting processes records for(int i = 0; i