mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
Coments and tidy
This commit is contained in:
parent
b5c5f4904d
commit
d4faf80797
@ -1,7 +1,7 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class Scheduler {
|
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/datafile2.txt";
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ public class Scheduler {
|
|||||||
* Main. Runs the program
|
* Main. Runs the program
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
Scheduler system = new Scheduler();
|
A1 system = new A1();
|
||||||
system.run();
|
system.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,16 +28,17 @@ public class Scheduler {
|
|||||||
readFile(fileName); //Read the file
|
readFile(fileName); //Read the file
|
||||||
//printFile();
|
//printFile();
|
||||||
|
|
||||||
FCFS fcfs = new FCFS(disp, processList);
|
Algorithm fcfs = new Algorithm(disp, processList, "FCFS");
|
||||||
fcfs.runDispatch();
|
fcfs.runDispatch();
|
||||||
fcfs.printResults();
|
printResults();
|
||||||
reset();
|
reset();
|
||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
SRT srt = new SRT(disp, processList);
|
Algorithm srt = new Algorithm(disp, processList, "SRT");
|
||||||
srt.runDispatch();
|
srt.runDispatch();
|
||||||
srt.printResults();
|
printResults();
|
||||||
|
reset();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +49,17 @@ public class Scheduler {
|
|||||||
processList.get(i).reset();
|
processList.get(i).reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printResults()
|
||||||
|
{
|
||||||
|
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
|
||||||
|
for (int i = 0; i < processList.size(); i++)
|
||||||
|
{
|
||||||
|
System.out.print(processList.get(i).getId()+"\t\t");
|
||||||
|
System.out.print(processList.get(i).getTurnTime()+"\t\t\t");
|
||||||
|
System.out.print(processList.get(i).getWaitTime()+"\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* readFile. Reads the file given, creating processes as needed to save details.
|
* readFile. Reads the file given, creating processes as needed to save details.
|
||||||
* @param fileNameInput address of the file to be read in.
|
* @param fileNameInput address of the file to be read in.
|
||||||
@ -1,23 +1,22 @@
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
/**
|
public class Algorithm {
|
||||||
* Shortest Remaining Time
|
|
||||||
*/
|
|
||||||
public class FCFS {
|
|
||||||
|
|
||||||
private int timeStep = 0;
|
private int timeStep = 0;
|
||||||
private int disp;
|
private int disp;
|
||||||
private int timeLastChecked = -1;
|
private int timeLastChecked = -1;
|
||||||
|
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 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
|
||||||
|
|
||||||
//Constructors
|
//Constructor
|
||||||
public FCFS(int dispInput, LinkedList<Process> processListInput)
|
public Algorithm(int dispInput, LinkedList<Process> processListInput, String typeOfAlgorithm)
|
||||||
{
|
{
|
||||||
disp = dispInput;
|
disp = dispInput;
|
||||||
processListFromFile = processListInput;
|
processListFromFile = processListInput;
|
||||||
|
type = typeOfAlgorithm;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check for new processes that have arrived
|
//Check for new processes that have arrived
|
||||||
@ -64,7 +63,7 @@ public class FCFS {
|
|||||||
//Run the actual dispatch and running of programs
|
//Run the actual dispatch and running of programs
|
||||||
public void runDispatch()
|
public void runDispatch()
|
||||||
{
|
{
|
||||||
System.out.println("FCFS:"); //Currently using FCFS methodology
|
System.out.println(type+":"); //Currently using FCFS methodology
|
||||||
|
|
||||||
int processesFinished = 0; //Used to track if we've done all the processes from the file
|
int processesFinished = 0; //Used to track if we've done all the processes from the file
|
||||||
|
|
||||||
@ -78,8 +77,19 @@ public class FCFS {
|
|||||||
//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)
|
||||||
{
|
{
|
||||||
|
//Which process runs next depends on the algorithm used.
|
||||||
|
switch (type) {
|
||||||
|
case "FCFS":
|
||||||
|
currentProcess = waitingQueue.getFirst();
|
||||||
|
break;
|
||||||
|
case "SRT":
|
||||||
|
currentProcess = findShortestProcess();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
currentProcess = waitingQueue.getFirst();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
currentProcess = findNextProcess();
|
|
||||||
|
|
||||||
//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 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()))
|
if(!lastRunProcess.getId().equals(currentProcess.getId()))
|
||||||
@ -95,8 +105,10 @@ public class FCFS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runProcessTick(currentProcess);
|
//Move the simulation one step forward
|
||||||
|
progressOneTick(currentProcess);
|
||||||
|
|
||||||
|
//If a process finishes, save details and remove it from the queue
|
||||||
if(currentProcess.isDone())
|
if(currentProcess.isDone())
|
||||||
{
|
{
|
||||||
currentProcess.saveEndTime(timeStep);
|
currentProcess.saveEndTime(timeStep);
|
||||||
@ -111,36 +123,44 @@ public class FCFS {
|
|||||||
}while(processesFinished!=processListFromFile.size());
|
}while(processesFinished!=processListFromFile.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Process findNextProcess()
|
/**
|
||||||
|
* Finds and returns the process with the shortest remaining time till completion
|
||||||
|
* @return the process with the shortest remaining time till completion
|
||||||
|
*/
|
||||||
|
private Process findShortestProcess()
|
||||||
{
|
{
|
||||||
// int listPos = -1;
|
int listPos = -1;
|
||||||
// int remaining = waitingQueue.getFirst().getRemainingTime();
|
int remaining = waitingQueue.getFirst().getRemainingTime();
|
||||||
|
|
||||||
// for(int i = 0; i<waitingQueue.size(); i++)
|
for(int i = 0; i<waitingQueue.size(); i++)
|
||||||
// {
|
{
|
||||||
// if(waitingQueue.get(i).getRemainingTime()<remaining)
|
if(waitingQueue.get(i).getRemainingTime()<remaining)
|
||||||
// {
|
{
|
||||||
// listPos = i;
|
listPos = i;
|
||||||
// remaining = waitingQueue.get(i).getRemainingTime();
|
remaining = waitingQueue.get(i).getRemainingTime();
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// if(listPos==-1)
|
|
||||||
// {
|
|
||||||
// return waitingQueue.getFirst();
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
|
|
||||||
return waitingQueue.getFirst();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runProcessTick(Process input)
|
if(listPos==-1)
|
||||||
|
{
|
||||||
|
return waitingQueue.getFirst();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return waitingQueue.get(listPos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moves the simulation along 1 tick
|
||||||
|
* @param currentlyRunningProcess Is the process that is currently running. It gets 1 progress tick, everything else waiting gets 1 waiting tick.
|
||||||
|
*/
|
||||||
|
private void progressOneTick(Process currentlyRunningProcess)
|
||||||
{
|
{
|
||||||
timeStep++;
|
timeStep++;
|
||||||
for(int i = 0; i< waitingQueue.size(); i++)
|
for(int i = 0; i< waitingQueue.size(); i++)
|
||||||
{
|
{
|
||||||
if(!input.getId().equals(waitingQueue.get(i).getId()))
|
if(!currentlyRunningProcess.getId().equals(waitingQueue.get(i).getId()))
|
||||||
{
|
{
|
||||||
waitingQueue.get(i).runForOneTick(false);
|
waitingQueue.get(i).runForOneTick(false);
|
||||||
}
|
}
|
||||||
@ -150,15 +170,4 @@ public class FCFS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printResults()
|
|
||||||
{
|
|
||||||
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
|
|
||||||
for (int i = 0; i < processListFromFile.size(); i++)
|
|
||||||
{
|
|
||||||
System.out.print(processListFromFile.get(i).getId()+"\t\t");
|
|
||||||
System.out.print(processListFromFile.get(i).getTurnTime()+"\t\t\t");
|
|
||||||
System.out.print(processListFromFile.get(i).getWaitTime()+"\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
162
SRT.java
162
SRT.java
@ -1,162 +0,0 @@
|
|||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shortest Remaining Time
|
|
||||||
*/
|
|
||||||
public class SRT {
|
|
||||||
|
|
||||||
private int timeStep = 0;
|
|
||||||
private int disp;
|
|
||||||
private int timeLastChecked = -1;
|
|
||||||
|
|
||||||
private static LinkedList<Process> processListFromFile; //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 after each time point.
|
|
||||||
//Constructors
|
|
||||||
|
|
||||||
public SRT(int dispInput, LinkedList<Process> processListInput)
|
|
||||||
{
|
|
||||||
disp = dispInput;
|
|
||||||
processListFromFile = processListInput;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void checkForArrivals()
|
|
||||||
{
|
|
||||||
for(int i = 0; i< processListFromFile.size(); i++)
|
|
||||||
{
|
|
||||||
if((processListFromFile.get(i).getArrive()<=timeStep) && (processListFromFile.get(i).getArrive() > timeLastChecked))
|
|
||||||
{
|
|
||||||
newProcesses.add(processListFromFile.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
timeLastChecked = timeStep;
|
|
||||||
|
|
||||||
//do any tie breaking by sorting the new processes based on name
|
|
||||||
boolean swap = false;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
swap = false;
|
|
||||||
for(int i = 0; i< newProcesses.size()-1; i++)
|
|
||||||
{
|
|
||||||
if(Integer.parseInt(newProcesses.get(i).getId().substring(1))>Integer.parseInt(newProcesses.get(i+1).getId().substring(1)))
|
|
||||||
{
|
|
||||||
Process temp = newProcesses.get(i);
|
|
||||||
newProcesses.remove(i);
|
|
||||||
newProcesses.add(i+1, temp);
|
|
||||||
swap = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}while(swap);
|
|
||||||
|
|
||||||
//Move the newly arrived (and organised) processes to the queue.
|
|
||||||
for(int i =0; i < newProcesses.size(); i++)
|
|
||||||
{
|
|
||||||
waitingQueue.add(newProcesses.get(i));
|
|
||||||
}
|
|
||||||
newProcesses.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void runDispatch()
|
|
||||||
{
|
|
||||||
System.out.println("SRT:");
|
|
||||||
int processesFinished = 0;
|
|
||||||
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 = findShortestProcess();
|
|
||||||
|
|
||||||
if(!lastRunProcess.getId().equals(currentProcess.getId()))
|
|
||||||
{
|
|
||||||
timeStep = timeStep+disp;
|
|
||||||
System.out.println("T"+timeStep+": "+currentProcess.getId());
|
|
||||||
for(int i = 0; i<waitingQueue.size(); i++)
|
|
||||||
{
|
|
||||||
waitingQueue.get(i).runForOneTick(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
runProcessTick(currentProcess);
|
|
||||||
|
|
||||||
if(currentProcess.isDone())
|
|
||||||
{
|
|
||||||
waitingQueue.remove(currentProcess);
|
|
||||||
processesFinished++;
|
|
||||||
}
|
|
||||||
|
|
||||||
lastRunProcess = currentProcess;
|
|
||||||
}
|
|
||||||
|
|
||||||
}while(processesFinished!=processListFromFile.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
private Process findShortestProcess()
|
|
||||||
{
|
|
||||||
int listPos = -1;
|
|
||||||
int remaining = waitingQueue.getFirst().getRemainingTime();
|
|
||||||
|
|
||||||
for(int i = 0; i<waitingQueue.size(); i++)
|
|
||||||
{
|
|
||||||
if(waitingQueue.get(i).getRemainingTime()<remaining)
|
|
||||||
{
|
|
||||||
listPos = i;
|
|
||||||
remaining = waitingQueue.get(i).getRemainingTime();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(listPos==-1)
|
|
||||||
{
|
|
||||||
return waitingQueue.getFirst();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return waitingQueue.get(listPos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runProcessTick(Process input)
|
|
||||||
{
|
|
||||||
timeStep++;
|
|
||||||
for(int i = 0; i< waitingQueue.size(); i++)
|
|
||||||
{
|
|
||||||
if(!input.getId().equals(waitingQueue.get(i).getId()))
|
|
||||||
{
|
|
||||||
waitingQueue.get(i).runForOneTick(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
waitingQueue.get(i).runForOneTick(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(input.isDone())
|
|
||||||
{
|
|
||||||
input.saveEndTime(timeStep);
|
|
||||||
input.saveTurnTime();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void printResults()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < processListFromFile.size(); i++)
|
|
||||||
{
|
|
||||||
//System.out.println("T"+processListFromFile.get(i).getStartTime()+": "+processListFromFile.get(i).getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
|
|
||||||
for (int i = 0; i < processListFromFile.size(); i++)
|
|
||||||
{
|
|
||||||
System.out.print(processListFromFile.get(i).getId()+"\t\t");
|
|
||||||
System.out.print(processListFromFile.get(i).getTurnTime()+"\t\t\t");
|
|
||||||
System.out.print(processListFromFile.get(i).getWaitTime()+"\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user