From ae03995d4e3b308b4aa783a84c39376b2e66e634 Mon Sep 17 00:00:00 2001 From: Zach S-B Date: Sat, 28 Aug 2021 16:16:58 +1000 Subject: [PATCH] Mostly working LTR --- A1.java | 19 +++++++++++------ Algorithm.java | 56 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/A1.java b/A1.java index af50144..f8a952c 100644 --- a/A1.java +++ b/A1.java @@ -3,11 +3,10 @@ import java.io.*; public class A1 { //Address of File name relative to code. Replace with request for filename? - String fileName = "datafiles/datafile2.txt"; + String fileName = "datafiles/datafile1.txt"; LinkedList processList = new LinkedList(); LinkedList randomNum = new LinkedList<>(); - int randomNumConsumed = -1; //Counter to track which "random numbers" have been used thus far. //Initialise Dispatcher time variable int disp; @@ -28,17 +27,25 @@ public class A1 { readFile(fileName); //Read the file //printFile(); - Algorithm fcfs = new Algorithm(disp, processList, "FCFS"); + Algorithm fcfs = new Algorithm(disp, processList, randomNum, "FCFS"); fcfs.runDispatch(); printResults(); + System.out.println(fcfs.averageTurnTime()); + reset(); + + System.out.println(); + + Algorithm srt = new Algorithm(disp, processList, randomNum,"SRT"); + srt.runDispatch(); + printResults(); reset(); System.out.println(); - Algorithm srt = new Algorithm(disp, processList, "SRT"); - srt.runDispatch(); + Algorithm ltr = new Algorithm(disp, processList, randomNum,"LTR"); + ltr.runDispatch(); printResults(); - reset(); + reset(); } diff --git a/Algorithm.java b/Algorithm.java index 16e5a0f..4617e45 100644 --- a/Algorithm.java +++ b/Algorithm.java @@ -8,15 +8,17 @@ public class Algorithm { private String type; private static LinkedList processListFromFile; //Original List of processes from the the file + private static LinkedList randomFromFile; //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) + public Algorithm(int dispInput, LinkedList processListInput, LinkedList randomListInput, String typeOfAlgorithm) { disp = dispInput; processListFromFile = processListInput; type = typeOfAlgorithm; + randomFromFile = randomListInput; } //Check for new processes that have arrived @@ -73,7 +75,6 @@ public class Algorithm { do { checkForArrivals(); //Check for any new processes since timestep - //As long as there is something in the queue if(waitingQueue.size()>0) { @@ -85,6 +86,10 @@ public class Algorithm { case "SRT": currentProcess = findShortestProcess(); break; + + case "LTR": + currentProcess = getWinner(); + break; default: currentProcess = waitingQueue.getFirst(); break; @@ -169,5 +174,50 @@ public class Algorithm { waitingQueue.get(i).runForOneTick(true); } } - } + } + + int randIndex = 0; + private int getRandom() + { + int returnvalue; + returnvalue = randomFromFile.get(randIndex); + randIndex++; + + if(randIndex > randomFromFile.size()) + { + randIndex = 0; + } + + return returnvalue; + } + + private Process getWinner() + { + int counter = 0; + + int winner = getRandom(); + + while(true) + { + for(int i = 0; i < waitingQueue.size(); i++) + { + counter+=waitingQueue.get(i).getTickets(); + if(counter>winner) + { + return waitingQueue.get(i); + } + } + } + } + + public double averageTurnTime() + { + double turnTime = 0; + for(int i = 0; i