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
123
FCFS.java
123
FCFS.java
@ -1,38 +1,56 @@
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* First Come First Serve Algorithm
|
||||
*/
|
||||
public class FCFS {
|
||||
|
||||
private int timeStep = 0;
|
||||
private int current = 0;
|
||||
private int disp;
|
||||
private int timeLastDisp = -1;
|
||||
private int timeLastChecked = -1;
|
||||
|
||||
LinkedList<Process> processList = new LinkedList<>();
|
||||
LinkedList<Process> waitingQueue = new LinkedList<>();
|
||||
LinkedList<Process> newProcesses = new LinkedList<>();
|
||||
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 FCFS(int dispInput, LinkedList<Process> processListInput)
|
||||
{
|
||||
disp = dispInput;
|
||||
processList = processListInput;
|
||||
processListFromFile = processListInput;
|
||||
}
|
||||
|
||||
|
||||
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(processList.get(i));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
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));
|
||||
@ -43,46 +61,71 @@ public class FCFS {
|
||||
|
||||
public void runDispatch()
|
||||
{
|
||||
System.out.println("FCFS:");
|
||||
do
|
||||
{
|
||||
timeStep+=disp;
|
||||
checkForArrivals();
|
||||
timeLastDisp = timeStep;
|
||||
runProcess(waitingQueue.removeFirst());
|
||||
}while(waitingQueue.size()!=0);
|
||||
System.out.println("FCFS:");
|
||||
int processesFinished = 0;
|
||||
boolean startNew = true;
|
||||
//timeStep++;
|
||||
|
||||
//...
|
||||
printResults();
|
||||
do
|
||||
{
|
||||
if(startNew)
|
||||
{
|
||||
timeStep = timeStep+disp;
|
||||
startNew = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
checkForArrivals();
|
||||
|
||||
if(waitingQueue.size()>0)
|
||||
{
|
||||
if(waitingQueue.getFirst().isDone())
|
||||
{
|
||||
waitingQueue.removeFirst();
|
||||
processesFinished++;
|
||||
startNew = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
runProcessTick(waitingQueue.getFirst());
|
||||
}
|
||||
}
|
||||
|
||||
}while(processesFinished!=processListFromFile.size());
|
||||
}
|
||||
|
||||
private void runProcess(Process input)
|
||||
private void runProcessTick(Process input)
|
||||
{
|
||||
input.saveStartTime(timeStep);
|
||||
|
||||
int startTime = timeStep;
|
||||
int endTime = -1;
|
||||
timeStep++;
|
||||
|
||||
System.out.println("T"+startTime+": "+input.getId());
|
||||
input.runForOneTick();
|
||||
|
||||
for(int i = 0; i < input.getExecSize(); i++)
|
||||
if(input.isDone())
|
||||
{
|
||||
timeStep++;
|
||||
input.saveEndTime(timeStep);
|
||||
input.saveTurnTime();
|
||||
input.saveWaitTime();
|
||||
}
|
||||
|
||||
endTime = timeStep;
|
||||
|
||||
input.saveTurnTime(endTime-input.getArrive());
|
||||
input.saveWaitTime(startTime-input.getArrive());
|
||||
}
|
||||
|
||||
public void printResults()
|
||||
{
|
||||
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
|
||||
for (int i = 0; i < processList.size(); i++)
|
||||
for (int i = 0; i < processListFromFile.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");
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
91
Process.java
91
Process.java
@ -6,8 +6,13 @@ public class Process {
|
||||
private int tickets;
|
||||
|
||||
//Post Run details
|
||||
private int turnAroundTime;
|
||||
private int waitTime;
|
||||
private int turnAroundTime = -1;
|
||||
private int waitTime = -1;
|
||||
private int startTime = -1;
|
||||
private int endTime= -1;
|
||||
|
||||
private int remainingTime;
|
||||
|
||||
|
||||
//Constructors
|
||||
public Process(){
|
||||
@ -18,58 +23,108 @@ public class Process {
|
||||
arrive = arriveInput;
|
||||
execSize = execSizeInput;
|
||||
tickets = ticketsInput;
|
||||
remainingTime = execSizeInput;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setId(String idInput){
|
||||
id = idInput;
|
||||
}
|
||||
|
||||
public void setArrive(int arriveInput){
|
||||
arrive = arriveInput;
|
||||
}
|
||||
|
||||
public void setSize(int execSizeInput){
|
||||
execSize = execSizeInput;
|
||||
remainingTime = execSizeInput;
|
||||
}
|
||||
public void setTickets(int 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)
|
||||
{
|
||||
waitTime = waitTimeInput;
|
||||
public void saveTurnTime(){
|
||||
turnAroundTime = endTime-arrive;
|
||||
}
|
||||
|
||||
public void saveWaitTime(){
|
||||
waitTime = startTime - arrive;
|
||||
}
|
||||
|
||||
|
||||
//Getters
|
||||
public String getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getArrive(){
|
||||
return arrive;
|
||||
}
|
||||
|
||||
public int getExecSize(){
|
||||
return execSize;
|
||||
}
|
||||
|
||||
public int getTickets(){
|
||||
return tickets;
|
||||
}
|
||||
|
||||
public int getTurnTime()
|
||||
{
|
||||
public int getTurnTime(){
|
||||
return turnAroundTime;
|
||||
}
|
||||
|
||||
public int getWaitTime()
|
||||
{
|
||||
public int getWaitTime(){
|
||||
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
|
||||
//printFile();
|
||||
|
||||
FCFS fcfs = new FCFS(disp, processList);
|
||||
fcfs.runDispatch();
|
||||
// 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.
|
||||
@ -200,23 +215,6 @@ public class Scheduler {
|
||||
{
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user