mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
pre-unidispatch
This commit is contained in:
parent
83ed551b44
commit
b5c5f4904d
44
FCFS.java
44
FCFS.java
@ -11,18 +11,19 @@ public class FCFS {
|
||||
|
||||
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
|
||||
private LinkedList<Process> newProcesses = new LinkedList<>(); //Processes that have arrived since the last check
|
||||
|
||||
//Constructors
|
||||
public FCFS(int dispInput, LinkedList<Process> processListInput)
|
||||
{
|
||||
disp = dispInput;
|
||||
processListFromFile = processListInput;
|
||||
}
|
||||
|
||||
|
||||
//Check for new processes that have arrived
|
||||
private void checkForArrivals()
|
||||
{
|
||||
//Check each processe from the file, and if it has arrived between timeStep and whenever we last checked, add it.
|
||||
for(int i = 0; i< processListFromFile.size(); i++)
|
||||
{
|
||||
if((processListFromFile.get(i).getArrive()<=timeStep) && (processListFromFile.get(i).getArrive() > timeLastChecked))
|
||||
@ -55,13 +56,18 @@ public class FCFS {
|
||||
{
|
||||
waitingQueue.add(newProcesses.get(i));
|
||||
}
|
||||
|
||||
//Clear the processes list so it is ready to go for the next time
|
||||
newProcesses.clear();
|
||||
}
|
||||
|
||||
//Run the actual dispatch and running of programs
|
||||
public void runDispatch()
|
||||
{
|
||||
System.out.println("FCFS:");
|
||||
int processesFinished = 0;
|
||||
System.out.println("FCFS:"); //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
|
||||
|
||||
@ -72,15 +78,20 @@ public class FCFS {
|
||||
//As long as there is something in the queue
|
||||
if(waitingQueue.size()>0)
|
||||
{
|
||||
currentProcess = findShortestProcess();
|
||||
|
||||
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(!lastRunProcess.getId().equals(currentProcess.getId()))
|
||||
{
|
||||
timeStep = timeStep+disp;
|
||||
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).runForOneTick(false);
|
||||
waitingQueue.get(i).wait(disp);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,6 +99,8 @@ public class FCFS {
|
||||
|
||||
if(currentProcess.isDone())
|
||||
{
|
||||
currentProcess.saveEndTime(timeStep);
|
||||
currentProcess.saveTurnTime();
|
||||
waitingQueue.remove(currentProcess);
|
||||
processesFinished++;
|
||||
}
|
||||
@ -98,7 +111,7 @@ public class FCFS {
|
||||
}while(processesFinished!=processListFromFile.size());
|
||||
}
|
||||
|
||||
private Process findShortestProcess()
|
||||
private Process findNextProcess()
|
||||
{
|
||||
// int listPos = -1;
|
||||
// int remaining = waitingQueue.getFirst().getRemainingTime();
|
||||
@ -118,8 +131,8 @@ public class FCFS {
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
return waitingQueue.getFirst();
|
||||
|
||||
return waitingQueue.getFirst();
|
||||
}
|
||||
|
||||
private void runProcessTick(Process input)
|
||||
@ -136,21 +149,10 @@ public class FCFS {
|
||||
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++)
|
||||
{
|
||||
|
||||
21
Process.java
21
Process.java
@ -110,14 +110,19 @@ public class Process {
|
||||
}
|
||||
else
|
||||
{
|
||||
if(waitTime==-1)
|
||||
{
|
||||
waitTime=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
waitTime++;
|
||||
}
|
||||
wait(1);
|
||||
}
|
||||
}
|
||||
|
||||
public void wait(int time)
|
||||
{
|
||||
if(waitTime==-1)
|
||||
{
|
||||
waitTime=time;
|
||||
}
|
||||
else
|
||||
{
|
||||
waitTime+= time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user