Mostly working LTR

This commit is contained in:
Zach S-B 2021-08-28 16:16:58 +10:00
parent d4faf80797
commit ae03995d4e
2 changed files with 66 additions and 9 deletions

19
A1.java
View File

@ -3,11 +3,10 @@ import java.io.*;
public class A1 { public class A1 {
//Address of File name relative to code. Replace with request for filename? //Address of File name relative to code. Replace with request for filename?
String fileName = "datafiles/datafile2.txt"; String fileName = "datafiles/datafile1.txt";
LinkedList<Process> processList = new LinkedList<Process>(); LinkedList<Process> processList = new LinkedList<Process>();
LinkedList<Integer> randomNum = new LinkedList<>(); LinkedList<Integer> randomNum = new LinkedList<>();
int randomNumConsumed = -1; //Counter to track which "random numbers" have been used thus far.
//Initialise Dispatcher time variable //Initialise Dispatcher time variable
int disp; int disp;
@ -28,17 +27,25 @@ public class A1 {
readFile(fileName); //Read the file readFile(fileName); //Read the file
//printFile(); //printFile();
Algorithm fcfs = new Algorithm(disp, processList, "FCFS"); Algorithm fcfs = new Algorithm(disp, processList, randomNum, "FCFS");
fcfs.runDispatch(); fcfs.runDispatch();
printResults(); printResults();
System.out.println(fcfs.averageTurnTime());
reset();
System.out.println();
Algorithm srt = new Algorithm(disp, processList, randomNum,"SRT");
srt.runDispatch();
printResults();
reset(); reset();
System.out.println(); System.out.println();
Algorithm srt = new Algorithm(disp, processList, "SRT"); Algorithm ltr = new Algorithm(disp, processList, randomNum,"LTR");
srt.runDispatch(); ltr.runDispatch();
printResults(); printResults();
reset(); reset();
} }

View File

@ -8,15 +8,17 @@ public class Algorithm {
private String type; private String type;
private static LinkedList<Process> processListFromFile; //Original List of processes from the the file private static LinkedList<Process> processListFromFile; //Original List of processes from the the file
private static LinkedList<Integer> randomFromFile; //Original List of processes from the the file
private LinkedList<Process> waitingQueue = new LinkedList<>(); //Processes officially added to the waiting queue. private LinkedList<Process> waitingQueue = new LinkedList<>(); //Processes officially added to the waiting queue.
private LinkedList<Process> newProcesses = new LinkedList<>(); //Processes that have arrived since the last check private LinkedList<Process> newProcesses = new LinkedList<>(); //Processes that have arrived since the last check
//Constructor //Constructor
public Algorithm(int dispInput, LinkedList<Process> processListInput, String typeOfAlgorithm) public Algorithm(int dispInput, LinkedList<Process> processListInput, LinkedList<Integer> randomListInput, String typeOfAlgorithm)
{ {
disp = dispInput; disp = dispInput;
processListFromFile = processListInput; processListFromFile = processListInput;
type = typeOfAlgorithm; type = typeOfAlgorithm;
randomFromFile = randomListInput;
} }
//Check for new processes that have arrived //Check for new processes that have arrived
@ -73,7 +75,6 @@ public class Algorithm {
do do
{ {
checkForArrivals(); //Check for any new processes since timestep checkForArrivals(); //Check for any new processes since timestep
//As long as there is something in the queue //As long as there is something in the queue
if(waitingQueue.size()>0) if(waitingQueue.size()>0)
{ {
@ -85,6 +86,10 @@ public class Algorithm {
case "SRT": case "SRT":
currentProcess = findShortestProcess(); currentProcess = findShortestProcess();
break; break;
case "LTR":
currentProcess = getWinner();
break;
default: default:
currentProcess = waitingQueue.getFirst(); currentProcess = waitingQueue.getFirst();
break; break;
@ -169,5 +174,50 @@ public class Algorithm {
waitingQueue.get(i).runForOneTick(true); 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<processListFromFile.size(); i++)
{
turnTime+= processListFromFile.get(i).getTurnTime();
}
return turnTime/processListFromFile.size();
}
} }