mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
131 lines
3.8 KiB
Java
131 lines
3.8 KiB
Java
import java.util.LinkedList;
|
|
|
|
/**
|
|
* First Come First Serve Algorithm
|
|
*/
|
|
public class FCFS {
|
|
|
|
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 FCFS(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("FCFS:");
|
|
int processesFinished = 0;
|
|
boolean startNew = true;
|
|
//timeStep++;
|
|
|
|
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 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");
|
|
}
|
|
}
|
|
} |