mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
248 lines
7.7 KiB
Java
248 lines
7.7 KiB
Java
import java.util.*;
|
||
import java.io.*;
|
||
|
||
public class Scheduler {
|
||
//Address of File name relative to code. Replace with request for filename?
|
||
String fileName = "datafiles/datafile2.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;
|
||
|
||
/**
|
||
* Main. Runs the program
|
||
*/
|
||
public static void main(String[] args){
|
||
Scheduler system = new Scheduler();
|
||
system.run();
|
||
}
|
||
|
||
/**
|
||
* Run. Most of the program starts from here.
|
||
*/
|
||
public void run()
|
||
{
|
||
readFile(fileName); //Read the file
|
||
//printFile();
|
||
|
||
// FCFS fcfs = new FCFS(disp, processList);
|
||
//fcfs.runDispatch();
|
||
// fcfs.printResults();
|
||
//reset();
|
||
|
||
System.out.println();
|
||
|
||
SRT srt = new SRT(disp, processList);
|
||
srt.runDispatch();
|
||
srt.printResults();
|
||
|
||
}
|
||
|
||
public void reset()
|
||
{
|
||
for(int i = 0; i < processList.size(); i++)
|
||
{
|
||
processList.get(i).reset();
|
||
}
|
||
}
|
||
/**
|
||
* readFile. Reads the file given, creating processes as needed to save details.
|
||
* @param fileNameInput address of the file to be read in.
|
||
*/
|
||
public void readFile(String fileNameInput)
|
||
{
|
||
Scanner fileReader; //Scanner to read the file.
|
||
File dataSet = new File(fileNameInput);
|
||
|
||
|
||
//Try and open file. If it works, procede.
|
||
try {
|
||
fileReader = new Scanner(dataSet);
|
||
|
||
|
||
//Progress through each line in the text file
|
||
while(fileReader.hasNextLine()){
|
||
String line = fileReader.nextLine();
|
||
|
||
//If the line has "EOF" written, stop reading lines.
|
||
if(line.equals("EOF"))
|
||
{
|
||
|
||
break;
|
||
}
|
||
|
||
else if(line.startsWith("DISP"))
|
||
{
|
||
disp =Integer.parseInt(line.substring(6));
|
||
}
|
||
|
||
//If the Line starts with "ID:", create a new process
|
||
else if(line.startsWith("ID:"))
|
||
{
|
||
//Create new proccess
|
||
Process temp = new Process();
|
||
|
||
|
||
temp.setId(line.substring(4)); //Set the id of the proccess to whatever remains of the line after the "ID: "
|
||
|
||
//As we've read in the ID, we shall assume the following lines (until "END") are all part of the process definition.
|
||
while(fileReader.hasNextLine())
|
||
{
|
||
line = fileReader.nextLine();
|
||
|
||
if(line.equals("END"))
|
||
{
|
||
break;
|
||
}
|
||
|
||
else if(line.startsWith("Arrive:"))
|
||
{
|
||
temp.setArrive(Integer.parseInt(line.substring(8)));
|
||
}
|
||
|
||
else if(line.startsWith("ExecSize:"))
|
||
{
|
||
|
||
temp.setSize(Integer.parseInt(line.substring(10)));
|
||
}
|
||
|
||
else if(line.startsWith("Tickets:"))
|
||
{
|
||
temp.setTickets(Integer.parseInt(line.substring(9)));
|
||
}
|
||
|
||
else
|
||
{
|
||
//Delete process?
|
||
}
|
||
}
|
||
|
||
processList.add(temp);
|
||
}
|
||
|
||
//If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line,
|
||
//we'll just keep reading in each number (and it is assumed each line is a single number) and adding it
|
||
//to the random number array.
|
||
else if(line.equals("BEGINRANDOM"))
|
||
{
|
||
while(fileReader.hasNextLine())
|
||
{
|
||
line = fileReader.nextLine();
|
||
|
||
if(line.equals("ENDRANDOM"))
|
||
{
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
randomNum.add(Integer.parseInt(line));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
fileReader.close();
|
||
}
|
||
|
||
//If the file isn't found, throw a hissy fit.
|
||
catch (FileNotFoundException e) {
|
||
System.out.println("File not found.");
|
||
//e.printStackTrace(); //Optional, shows more data.
|
||
}
|
||
}
|
||
|
||
public void printFile()
|
||
{
|
||
//Test Print
|
||
System.out.println("DISP: " + disp + "\n");
|
||
|
||
System.out.println("Begin Process List:");
|
||
|
||
for (int i = 0; i < processList.size() ; i++)
|
||
{
|
||
System.out.println("ID: " + processList.get(i).getId());
|
||
System.out.println("Arrive: " + processList.get(i).getArrive());
|
||
System.out.println("ExecSize: " + processList.get(i).getExecSize());
|
||
System.out.println("Tickets: " + processList.get(i).getTickets());
|
||
System.out.println();
|
||
}
|
||
System.out.println("End Process List.\n");
|
||
|
||
System.out.println("Begin Random:");
|
||
for (int i = 0; i < randomNum.size(); i++)
|
||
{
|
||
System.out.println(randomNum.get(i));
|
||
}
|
||
System.out.println("End Random.");
|
||
}
|
||
|
||
|
||
public void algorithmLTR()
|
||
{
|
||
// counter: used to track if we’ve found the winner yet
|
||
int counter = 0;
|
||
int totalTickets = 0;
|
||
int winner = -1;
|
||
|
||
//count the total number of tickets in play
|
||
for (int i = 0; i < processList.size() ; i++)
|
||
{
|
||
totalTickets += processList.get(i).getTickets();
|
||
}
|
||
|
||
// winner: use some call to a random number generator to
|
||
// get a value, between 0 and the total # of tickets
|
||
int winnerValue = returnRandomNum(totalTickets);
|
||
|
||
for (int i = 0; i < processList.size(); i++)
|
||
{
|
||
counter = processList.get(i).getTickets();
|
||
|
||
if(counter > winnerValue)
|
||
{
|
||
winner = i;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if(winner == -1)
|
||
{
|
||
System.out.println("No winner");
|
||
}
|
||
else
|
||
{
|
||
System.out.println("Winner: " + processList.get(winner).getId());
|
||
}
|
||
}
|
||
|
||
public int returnRandomNum(int totalTickets)
|
||
{
|
||
|
||
/*
|
||
Friendly neighbourhood Latch — 08/08/2021
|
||
I figured it out
|
||
Keep looping through the list of unfinished processes, adding that process's number of tickets to the counter, until you get to one that is greater than the winning number. If you get to the end of the list, jump back to the start and keep looping through
|
||
At the moment it's a disgusting while loop I'm wondering if there's a way I can do that a bit nicer because while loops give me bad vibes but it does the trick
|
||
*/
|
||
int randomValue = 0;
|
||
|
||
|
||
//Loop back to begining. Probably could remove each consumed random number from the linked list.... but I don't want to right now.
|
||
if((randomNumConsumed+1) >= randomNum.size())
|
||
{
|
||
randomNumConsumed = 0;
|
||
}
|
||
else
|
||
{
|
||
randomNumConsumed += 1;
|
||
}
|
||
|
||
randomValue = randomNum.get(randomNumConsumed) % totalTickets;
|
||
|
||
return randomValue;
|
||
}
|
||
|
||
|
||
} |