Submitted

This commit is contained in:
Zach S-B 2021-08-28 18:30:41 +10:00
parent 35d1d2eff3
commit 25e002c963
3 changed files with 226 additions and 65 deletions

71
A1.java
View File

@ -1,3 +1,10 @@
/**
* COMP2240 Assignment 1
* Date: 28/08/21
* Author: Zach S-B
* Student Number: c3262201
*/
import java.util.*;
import java.io.*;
@ -25,15 +32,15 @@ public class A1 {
public void run()
{
System.out.println("COMP2240 - Assignment 1 \nAuthor: Zach S-B \nStudent# :c3262201");
System.out.println("NOTE: Sadly, I did not do FBV.");
System.out.println("NOTE: See comment on the moveQueues function, short version is its broken");
//Store results for final print in these arrays. [0] is turnaround average, [1] is waiting time average
double[] fcfsResults = new double[2];
double[] srtResults = new double[2];
double[] fbvResults = new double[2];
double[] ltrResults = new double[2];
readFile(); //Read the file
//printFile();
readFile();
Algorithm fcfs = new Algorithm(disp, processList, randomNum, "FCFS");
fcfs.runDispatch();
@ -51,6 +58,13 @@ public class A1 {
printResults();
reset();
Algorithm fbv = new Algorithm(disp, processList, randomNum,"FBV");
fbv.runDispatchFBV();
fbvResults[0] = fbv.averageTurnTime();
fbvResults[1] = fbv.averageWaitTime();
printResults();
reset();
System.out.println();
Algorithm ltr = new Algorithm(disp, processList, randomNum,"LTR");
@ -60,7 +74,7 @@ public class A1 {
printResults();
reset();
printAverages(fcfsResults, srtResults, ltrResults);
printAverages(fcfsResults, srtResults, fbvResults, ltrResults);
}
@ -72,17 +86,28 @@ public class A1 {
}
}
public void printAverages(double[] fcfs, double[] srt, double[] ltr)
/**
* Prints out the averages of each algorithm. It assumes each one has been run at least once.
* @param fcfs First Come Firs Serve, assumes first value in array is Turn around time, and second is the Waiting Time
* @param srt Shortest Remaining Time, assumes first value in array is Turn around time, and second is the Waiting Time
* @param fbv Feedback, assumes first value in array is Turn around time, and second is the Waiting Time
* @param ltr Lottery, assumes first value in array is Turn around time, and second is the Waiting Time
*/
public void printAverages(double[] fcfs, double[] srt, double[] fbv, double[] ltr)
{
System.out.println("\nAlgorithm \tAverage Turnaround Time \tAverage Waiting Time");
System.out.printf("FCFS\t\t%.2f\t\t\t\t%.2f\n",fcfs[0],fcfs[1]);
System.out.printf("SRT\t\t%.2f\t\t\t\t%.2f\n",srt[0],srt[1]);
System.out.printf("FBV\t\t-\t\t\t\t-\n");
System.out.printf("FBV\t\t%.2f\t\t\t\t%.2f\n",fbv[0],fbv[1]);
System.out.printf("LTR\t\t%.2f\t\t\t\t%.2f\n",ltr[0],ltr[1]);
}
/**
* Print the results about the last run values.
*/
public void printResults()
{
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
@ -93,7 +118,7 @@ public class A1 {
System.out.print(processList.get(i).getWaitTime()+"\n");
}
}
/**
/**
* readFile. Reads the file given, creating processes as needed to save details.
* @param fileNameInput address of the file to be read in.
*/
@ -197,7 +222,6 @@ public class A1 {
}
}
fileReader.close();
}
//If the file isn't found, throw a hissy fit.
@ -206,32 +230,7 @@ public class A1 {
//e.printStackTrace(); //Optional, shows more data.
}
}
//Close the scanner
sc.close();
}
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.");
}
}
}

View File

@ -1,3 +1,9 @@
/**
* COMP2240 Assignment 1
* Date: 28/08/21
* Author: Zach S-B
* Student Number: c3262201
*/
import java.util.LinkedList;
public class Algorithm {
@ -9,8 +15,14 @@ public class Algorithm {
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. Also functions as the first q1 for fbv
private LinkedList<Process> newProcesses = new LinkedList<>(); //Processes that have arrived since the last check
//Queues for FBV
private LinkedList<Process> q2 = new LinkedList<>();
private LinkedList<Process> q3 = new LinkedList<>();
private LinkedList<Process> q4 = new LinkedList<>();
//Constructor
public Algorithm(int dispInput, LinkedList<Process> processListInput, LinkedList<Integer> randomListInput, String typeOfAlgorithm)
@ -86,16 +98,14 @@ public class Algorithm {
case "SRT":
currentProcess = findShortestProcess();
break;
case "LTR":
currentProcess = getWinner();
break;
break;
default:
currentProcess = waitingQueue.getFirst();
break;
}
//If the next process to run is different than the last one run, we need to run the actual dispatcher, which takes time, to change process
if(!lastRunProcess.getId().equals(currentProcess.getId()))
{
@ -116,8 +126,8 @@ public class Algorithm {
//If a process finishes, save details and remove it from the queue
if(currentProcess.isDone())
{
currentProcess.saveEndTime(timeStep);
currentProcess.saveTurnTime();
currentProcess.setEndTime(timeStep);
currentProcess.setTurnTime();
waitingQueue.remove(currentProcess);
processesFinished++;
}
@ -128,6 +138,115 @@ public class Algorithm {
}while(processesFinished!=processListFromFile.size());
}
public void runDispatchFBV()
{
System.out.println(type+":"); //Currently using FCFS methodology
int processesFinished = 0; //Used to track if we've done all the processes from the file
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
//As long as there is something in the queue
if(waitingQueue.size()>0)
{
currentProcess = waitingQueue.getFirst();
//If the next process to run is different than the last one run, we need to run the actual dispatcher, which takes time, to change process
if(!lastRunProcess.getId().equals(currentProcess.getId()))
{
timeStep += disp;
System.out.println("T"+timeStep+": "+currentProcess.getId());
//Add the time waited to each of the currently waiting processes records
for(int i = 0; i<waitingQueue.size(); i++)
{
waitingQueue.get(i).wait(disp);
waitingQueue.get(i).addQueueTime(disp);
}
for(int i = 0; i<q2.size(); i++)
{
q2.get(i).wait(disp);
q2.get(i).addQueueTime(disp);
}
for(int i = 0; i<q3.size(); i++)
{
q3.get(i).wait(disp);
q3.get(i).addQueueTime(disp);
}
for(int i = 0; i<q4.size(); i++)
{
q4.get(i).wait(disp);
q4.get(i).addQueueTime(disp);
}
}
//Move the simulation one step forward
progressOneTick(currentProcess);
//If a process finishes, save details and remove it from the queue
if(currentProcess.isDone())
{
currentProcess.setEndTime(timeStep);
currentProcess.setTurnTime();
waitingQueue.remove(currentProcess);
processesFinished++;
}
moveQueues();
lastRunProcess = currentProcess;
}
}while(processesFinished!=processListFromFile.size());
}
/**
* Moves everything along in the queues. Frankly, this is quite wrong and I misunderstood the point of the time quantums.
* Instead of running for however many ticks, I let it stay in queue for a certain ammount of time (see q4, it has 4 instead of 32)
* Furthermore, I forgot to reset the queue wait time. If I fix this, it hangs. Clearly this function is lacking, but I do not have the time to correct it.
*/
private void moveQueues()
{
for(int i = 0; i<waitingQueue.size(); i++)
{
if(waitingQueue.get(i).getQueueTime()>=1)
{
Process temp = waitingQueue.remove(i);
q2.add(temp);
}
}
for(int i = 0; i<q2.size(); i++)
{
if(q2.get(i).getQueueTime()>=1)
{
Process temp = q2.remove(i);
q3.add(temp);
}
}
for(int i = 0; i<q3.size(); i++)
{
if(q3.get(i).getQueueTime()>=4)
{
Process temp = q3.remove(i);
q4.add(temp);
}
}
for(int i = 0; i<q4.size(); i++)
{
if(q4.get(i).getQueueTime()>=4)
{
Process temp = q4.remove(i);
waitingQueue.add(0, temp);
}
}
}
/**
* Finds and returns the process with the shortest remaining time till completion
* @return the process with the shortest remaining time till completion
@ -174,6 +293,42 @@ public class Algorithm {
waitingQueue.get(i).runForOneTick(true);
}
}
for(int i = 0; i< q2.size(); i++)
{
if(!currentlyRunningProcess.getId().equals(q2.get(i).getId()))
{
q2.get(i).runForOneTick(false);
}
else
{
q2.get(i).runForOneTick(true);
}
}
for(int i = 0; i< q3.size(); i++)
{
if(!currentlyRunningProcess.getId().equals(q3.get(i).getId()))
{
q3.get(i).runForOneTick(false);
}
else
{
q3.get(i).runForOneTick(true);
}
}
for(int i = 0; i< q4.size(); i++)
{
if(!currentlyRunningProcess.getId().equals(q4.get(i).getId()))
{
q4.get(i).runForOneTick(false);
}
else
{
q4.get(i).runForOneTick(true);
}
}
}
int randIndex = 0;

View File

@ -1,3 +1,10 @@
/**
* COMP2240 Assignment 1
* Date: 28/08/21
* Author: Zach S-B
* Student Number: c3262201
*/
public class Process {
private String id;
@ -5,13 +12,17 @@ public class Process {
private int execSize;
private int tickets;
private int remainingTime;
//Post Run details
private int turnAroundTime = -1;
private int waitTime = -1;
private int startTime = -1;
private int endTime= -1;
private int remainingTime;
//For FBV
private int inQueueTime = 0;
//Constructors
@ -40,34 +51,15 @@ public class Process {
public void setTickets(int ticketsInput){
tickets = ticketsInput;
}
/**
* Saves the time that the process first started running
* Only saves if there isn't already a valid time stored
* (ie, startTime == -1)
* @param timeInput
*/
public void saveStartTime(int timeInput)
{
if(startTime == -1)
{
startTime=timeInput;
}
}
public void saveEndTime(int timeInput)
public void setEndTime(int timeInput)
{
endTime=timeInput;
}
public void saveTurnTime(){
public void setTurnTime(){
turnAroundTime = endTime-arrive;
}
public void saveWaitTime(){
waitTime = startTime - arrive;
}
//Getters
public String getId(){
return id;
@ -99,6 +91,10 @@ public class Process {
{
return remainingTime;
}
public int getQueueTime()
{
return inQueueTime;
}
//Functions
@ -112,6 +108,7 @@ public class Process {
{
wait(1);
}
addQueueTime(1);
}
public void wait(int time)
@ -146,4 +143,14 @@ public class Process {
startTime = -1;
endTime = -1;
}
public void addQueueTime(int time)
{
inQueueTime+=time;
}
public void resetQueueTime()
{
inQueueTime=0;
}
}