mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
Started SRT
This commit is contained in:
parent
3fc10bbdd2
commit
0805a79ceb
139
FCFS.java
139
FCFS.java
@ -1,38 +1,56 @@
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* First Come First Serve Algorithm
|
||||||
|
*/
|
||||||
public class FCFS {
|
public class FCFS {
|
||||||
|
|
||||||
private int timeStep = 0;
|
private int timeStep = 0;
|
||||||
private int current = 0;
|
|
||||||
private int disp;
|
private int disp;
|
||||||
private int timeLastDisp = -1;
|
private int timeLastChecked = -1;
|
||||||
|
|
||||||
LinkedList<Process> processList = new LinkedList<>();
|
private static LinkedList<Process> processListFromFile; //Original List of processes from the the file
|
||||||
LinkedList<Process> waitingQueue = new LinkedList<>();
|
private LinkedList<Process> waitingQueue = new LinkedList<>(); //Processes officially added to the waiting queue.
|
||||||
LinkedList<Process> newProcesses = new LinkedList<>();
|
private LinkedList<Process> newProcesses = new LinkedList<>(); //Processes that have arrived after each time point.
|
||||||
//Constructors
|
//Constructors
|
||||||
|
|
||||||
public FCFS(int dispInput, LinkedList<Process> processListInput)
|
public FCFS(int dispInput, LinkedList<Process> processListInput)
|
||||||
{
|
{
|
||||||
disp = dispInput;
|
disp = dispInput;
|
||||||
processList = processListInput;
|
processListFromFile = processListInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void checkForArrivals()
|
private void checkForArrivals()
|
||||||
{
|
{
|
||||||
for(int i = 0; i< processList.size(); i++)
|
for(int i = 0; i< processListFromFile.size(); i++)
|
||||||
{
|
{
|
||||||
if((processList.get(i).getArrive()<=timeStep) && (processList.get(i).getArrive() > timeLastDisp))
|
if((processListFromFile.get(i).getArrive()<=timeStep) && (processListFromFile.get(i).getArrive() > timeLastChecked))
|
||||||
{
|
{
|
||||||
|
newProcesses.add(processListFromFile.get(i));
|
||||||
newProcesses.add(processList.get(i));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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++)
|
for(int i =0; i < newProcesses.size(); i++)
|
||||||
{
|
{
|
||||||
waitingQueue.add(newProcesses.get(i));
|
waitingQueue.add(newProcesses.get(i));
|
||||||
@ -43,46 +61,71 @@ public class FCFS {
|
|||||||
|
|
||||||
public void runDispatch()
|
public void runDispatch()
|
||||||
{
|
{
|
||||||
System.out.println("FCFS:");
|
System.out.println("FCFS:");
|
||||||
do
|
int processesFinished = 0;
|
||||||
{
|
boolean startNew = true;
|
||||||
timeStep+=disp;
|
//timeStep++;
|
||||||
checkForArrivals();
|
|
||||||
timeLastDisp = timeStep;
|
|
||||||
runProcess(waitingQueue.removeFirst());
|
|
||||||
}while(waitingQueue.size()!=0);
|
|
||||||
|
|
||||||
//...
|
|
||||||
printResults();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runProcess(Process input)
|
|
||||||
{
|
|
||||||
|
|
||||||
int startTime = timeStep;
|
do
|
||||||
int endTime = -1;
|
{
|
||||||
|
if(startNew)
|
||||||
System.out.println("T"+startTime+": "+input.getId());
|
{
|
||||||
|
timeStep = timeStep+disp;
|
||||||
for(int i = 0; i < input.getExecSize(); i++)
|
startNew = false;
|
||||||
{
|
}
|
||||||
timeStep++;
|
else
|
||||||
}
|
{
|
||||||
|
;
|
||||||
endTime = timeStep;
|
}
|
||||||
|
|
||||||
input.saveTurnTime(endTime-input.getArrive());
|
checkForArrivals();
|
||||||
input.saveWaitTime(startTime-input.getArrive());
|
|
||||||
|
if(waitingQueue.size()>0)
|
||||||
|
{
|
||||||
|
if(waitingQueue.getFirst().isDone())
|
||||||
|
{
|
||||||
|
waitingQueue.removeFirst();
|
||||||
|
processesFinished++;
|
||||||
|
startNew = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
runProcessTick(waitingQueue.getFirst());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}while(processesFinished!=processListFromFile.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void runProcessTick(Process input)
|
||||||
|
{
|
||||||
|
input.saveStartTime(timeStep);
|
||||||
|
|
||||||
|
timeStep++;
|
||||||
|
|
||||||
|
input.runForOneTick();
|
||||||
|
|
||||||
|
if(input.isDone())
|
||||||
|
{
|
||||||
|
input.saveEndTime(timeStep);
|
||||||
|
input.saveTurnTime();
|
||||||
|
input.saveWaitTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void printResults()
|
public void printResults()
|
||||||
{
|
{
|
||||||
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
|
for (int i = 0; i < processListFromFile.size(); i++)
|
||||||
for (int i = 0; i < processList.size(); i++)
|
|
||||||
{
|
{
|
||||||
System.out.print(processList.get(i).getId()+"\t\t");
|
System.out.println("T"+processListFromFile.get(i).getStartTime()+": "+processListFromFile.get(i).getId());
|
||||||
System.out.print(processList.get(i).getTurnTime()+"\t\t\t");
|
}
|
||||||
System.out.print(processList.get(i).getWaitTime()+"\n");
|
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
93
Process.java
93
Process.java
@ -6,8 +6,13 @@ public class Process {
|
|||||||
private int tickets;
|
private int tickets;
|
||||||
|
|
||||||
//Post Run details
|
//Post Run details
|
||||||
private int turnAroundTime;
|
private int turnAroundTime = -1;
|
||||||
private int waitTime;
|
private int waitTime = -1;
|
||||||
|
private int startTime = -1;
|
||||||
|
private int endTime= -1;
|
||||||
|
|
||||||
|
private int remainingTime;
|
||||||
|
|
||||||
|
|
||||||
//Constructors
|
//Constructors
|
||||||
public Process(){
|
public Process(){
|
||||||
@ -18,58 +23,108 @@ public class Process {
|
|||||||
arrive = arriveInput;
|
arrive = arriveInput;
|
||||||
execSize = execSizeInput;
|
execSize = execSizeInput;
|
||||||
tickets = ticketsInput;
|
tickets = ticketsInput;
|
||||||
|
remainingTime = execSizeInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Setters
|
//Setters
|
||||||
public void setId(String idInput){
|
public void setId(String idInput){
|
||||||
id = idInput;
|
id = idInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setArrive(int arriveInput){
|
public void setArrive(int arriveInput){
|
||||||
arrive = arriveInput;
|
arrive = arriveInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSize(int execSizeInput){
|
public void setSize(int execSizeInput){
|
||||||
execSize = execSizeInput;
|
execSize = execSizeInput;
|
||||||
|
remainingTime = execSizeInput;
|
||||||
}
|
}
|
||||||
public void setTickets(int ticketsInput){
|
public void setTickets(int ticketsInput){
|
||||||
tickets = ticketsInput;
|
tickets = ticketsInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveTurnTime(int turnTimeInput)
|
/**
|
||||||
|
* 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)
|
||||||
{
|
{
|
||||||
turnAroundTime = turnTimeInput;
|
if(startTime == -1)
|
||||||
|
{
|
||||||
|
startTime=timeInput;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void saveEndTime(int timeInput)
|
||||||
|
{
|
||||||
|
endTime=timeInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveWaitTime(int waitTimeInput)
|
public void saveTurnTime(){
|
||||||
{
|
turnAroundTime = endTime-arrive;
|
||||||
waitTime = waitTimeInput;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveWaitTime(){
|
||||||
|
waitTime = startTime - arrive;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Getters
|
//Getters
|
||||||
public String getId(){
|
public String getId(){
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getArrive(){
|
public int getArrive(){
|
||||||
return arrive;
|
return arrive;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getExecSize(){
|
public int getExecSize(){
|
||||||
return execSize;
|
return execSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTickets(){
|
public int getTickets(){
|
||||||
return tickets;
|
return tickets;
|
||||||
}
|
}
|
||||||
|
public int getTurnTime(){
|
||||||
public int getTurnTime()
|
|
||||||
{
|
|
||||||
return turnAroundTime;
|
return turnAroundTime;
|
||||||
}
|
}
|
||||||
|
public int getWaitTime(){
|
||||||
public int getWaitTime()
|
|
||||||
{
|
|
||||||
return waitTime;
|
return waitTime;
|
||||||
}
|
}
|
||||||
|
public int getStartTime()
|
||||||
|
{
|
||||||
|
return startTime;
|
||||||
|
}
|
||||||
|
public int getEndTime()
|
||||||
|
{
|
||||||
|
return endTime;
|
||||||
|
}
|
||||||
|
public int getRemainingTime()
|
||||||
|
{
|
||||||
|
return remainingTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
public void runForOneTick()
|
||||||
|
{
|
||||||
|
remainingTime--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDone()
|
||||||
|
{
|
||||||
|
if (remainingTime <= 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset()
|
||||||
|
{
|
||||||
|
turnAroundTime = -1;
|
||||||
|
waitTime = -1;
|
||||||
|
remainingTime = execSize;
|
||||||
|
startTime = -1;
|
||||||
|
endTime = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
147
SRT.java
Normal file
147
SRT.java
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
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;
|
||||||
|
boolean startNew = true;
|
||||||
|
//timeStep++;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if(startNew)
|
||||||
|
{
|
||||||
|
timeStep = timeStep+disp;
|
||||||
|
startNew = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkForArrivals();
|
||||||
|
|
||||||
|
if(waitingQueue.size()>0)
|
||||||
|
{
|
||||||
|
if(waitingQueue.getFirst().isDone())
|
||||||
|
{
|
||||||
|
waitingQueue.removeFirst();
|
||||||
|
processesFinished++;
|
||||||
|
startNew = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
runShortestProcess();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}while(processesFinished!=processListFromFile.size());
|
||||||
|
|
||||||
|
//...
|
||||||
|
printResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runShortestProcess()
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
int remaining = waitingQueue.get(0).getRemainingTime();
|
||||||
|
|
||||||
|
for(int i = 1; i<waitingQueue.size(); i++)
|
||||||
|
{
|
||||||
|
if(waitingQueue.get(i).getRemainingTime()<remaining)
|
||||||
|
{
|
||||||
|
pos = i;
|
||||||
|
remaining = waitingQueue.get(i).getRemainingTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runProcessTick(waitingQueue.get(pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runProcessTick(Process input)
|
||||||
|
{
|
||||||
|
input.saveStartTime(timeStep);
|
||||||
|
|
||||||
|
timeStep++;
|
||||||
|
|
||||||
|
input.runForOneTick();
|
||||||
|
|
||||||
|
if(input.isDone())
|
||||||
|
{
|
||||||
|
input.saveEndTime(timeStep);
|
||||||
|
input.saveTurnTime();
|
||||||
|
input.saveWaitTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -28,11 +28,26 @@ public class Scheduler {
|
|||||||
readFile(fileName); //Read the file
|
readFile(fileName); //Read the file
|
||||||
//printFile();
|
//printFile();
|
||||||
|
|
||||||
FCFS fcfs = new FCFS(disp, processList);
|
// FCFS fcfs = new FCFS(disp, processList);
|
||||||
fcfs.runDispatch();
|
//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.
|
* 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.
|
||||||
@ -200,23 +215,6 @@ public class Scheduler {
|
|||||||
{
|
{
|
||||||
System.out.println("Winner: " + processList.get(winner).getId());
|
System.out.println("Winner: " + processList.get(winner).getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
// current: use this to walk through the list of jobs
|
|
||||||
|
|
||||||
//node_t *current = head;
|
|
||||||
/*
|
|
||||||
while (current)
|
|
||||||
{
|
|
||||||
counter = counter + current->tickets;
|
|
||||||
if (counter > winner)
|
|
||||||
{
|
|
||||||
break; // found the winner
|
|
||||||
}
|
|
||||||
|
|
||||||
current = current->next;
|
|
||||||
// 'current' is the winner: schedule it...
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int returnRandomNum(int totalTickets)
|
public int returnRandomNum(int totalTickets)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user