mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
Mostly working LTR
This commit is contained in:
parent
d4faf80797
commit
ae03995d4e
17
A1.java
17
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<Process> processList = new LinkedList<Process>();
|
||||
LinkedList<Integer> randomNum = new LinkedList<>();
|
||||
int randomNumConsumed = -1; //Counter to track which "random numbers" have been used thus far.
|
||||
|
||||
//Initialise Dispatcher time variable
|
||||
int disp;
|
||||
@ -28,15 +27,23 @@ 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();
|
||||
|
||||
|
||||
@ -8,15 +8,17 @@ public class Algorithm {
|
||||
private String type;
|
||||
|
||||
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> newProcesses = new LinkedList<>(); //Processes that have arrived since the last check
|
||||
|
||||
//Constructor
|
||||
public Algorithm(int dispInput, LinkedList<Process> processListInput, String typeOfAlgorithm)
|
||||
public Algorithm(int dispInput, LinkedList<Process> processListInput, LinkedList<Integer> 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;
|
||||
@ -170,4 +175,49 @@ public class Algorithm {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user