import java.util.LinkedList; /** * Shortest Remaining Time */ public class SRT { private int timeStep = 0; private int disp; private int timeLastChecked = -1; 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 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 currentProcess = findShortestProcess(); //get the shortest one out of that //As long as there is something in the queue if(waitingQueue.size()>0) { if(!lastRunProcess.getId().equals(currentProcess.getId())) { timeStep = timeStep+disp; System.out.println("T"+timeStep+": "+currentProcess.getId()); for(int i = 0; i