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 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 after each time point.
|
private LinkedList<Process> newProcesses = new LinkedList<>(); //Processes that have arrived since the last check
|
||||||
//Constructors
|
|
||||||
|
|
||||||
|
//Constructors
|
||||||
public FCFS(int dispInput, LinkedList<Process> processListInput)
|
public FCFS(int dispInput, LinkedList<Process> processListInput)
|
||||||
{
|
{
|
||||||
disp = dispInput;
|
disp = dispInput;
|
||||||
processListFromFile = processListInput;
|
processListFromFile = processListInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Check for new processes that have arrived
|
||||||
private void checkForArrivals()
|
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++)
|
for(int i = 0; i< processListFromFile.size(); i++)
|
||||||
{
|
{
|
||||||
if((processListFromFile.get(i).getArrive()<=timeStep) && (processListFromFile.get(i).getArrive() > timeLastChecked))
|
if((processListFromFile.get(i).getArrive()<=timeStep) && (processListFromFile.get(i).getArrive() > timeLastChecked))
|
||||||
@ -55,13 +56,18 @@ public class FCFS {
|
|||||||
{
|
{
|
||||||
waitingQueue.add(newProcesses.get(i));
|
waitingQueue.add(newProcesses.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Clear the processes list so it is ready to go for the next time
|
||||||
newProcesses.clear();
|
newProcesses.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Run the actual dispatch and running of programs
|
||||||
public void runDispatch()
|
public void runDispatch()
|
||||||
{
|
{
|
||||||
System.out.println("FCFS:");
|
System.out.println("FCFS:"); //Currently using FCFS methodology
|
||||||
int processesFinished = 0;
|
|
||||||
|
int processesFinished = 0; //Used to track if we've done all the processes from the file
|
||||||
|
|
||||||
Process currentProcess;
|
Process currentProcess;
|
||||||
Process lastRunProcess = new Process("temp", 0, 0, 0); //temp process for comparing the first run
|
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
|
//As long as there is something in the queue
|
||||||
if(waitingQueue.size()>0)
|
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()))
|
if(!lastRunProcess.getId().equals(currentProcess.getId()))
|
||||||
{
|
{
|
||||||
timeStep = timeStep+disp;
|
timeStep += disp;
|
||||||
|
|
||||||
System.out.println("T"+timeStep+": "+currentProcess.getId());
|
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++)
|
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())
|
if(currentProcess.isDone())
|
||||||
{
|
{
|
||||||
|
currentProcess.saveEndTime(timeStep);
|
||||||
|
currentProcess.saveTurnTime();
|
||||||
waitingQueue.remove(currentProcess);
|
waitingQueue.remove(currentProcess);
|
||||||
processesFinished++;
|
processesFinished++;
|
||||||
}
|
}
|
||||||
@ -98,7 +111,7 @@ public class FCFS {
|
|||||||
}while(processesFinished!=processListFromFile.size());
|
}while(processesFinished!=processListFromFile.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Process findShortestProcess()
|
private Process findNextProcess()
|
||||||
{
|
{
|
||||||
// int listPos = -1;
|
// int listPos = -1;
|
||||||
// int remaining = waitingQueue.getFirst().getRemainingTime();
|
// int remaining = waitingQueue.getFirst().getRemainingTime();
|
||||||
@ -118,8 +131,8 @@ public class FCFS {
|
|||||||
// }
|
// }
|
||||||
// else
|
// else
|
||||||
// {
|
// {
|
||||||
return waitingQueue.getFirst();
|
|
||||||
|
|
||||||
|
return waitingQueue.getFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runProcessTick(Process input)
|
private void runProcessTick(Process input)
|
||||||
@ -136,21 +149,10 @@ public class FCFS {
|
|||||||
waitingQueue.get(i).runForOneTick(true);
|
waitingQueue.get(i).runForOneTick(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(input.isDone())
|
|
||||||
{
|
|
||||||
input.saveEndTime(timeStep);
|
|
||||||
input.saveTurnTime();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printResults()
|
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");
|
System.out.println("\nProcess \tTurnaround Time \tWaiting Time");
|
||||||
for (int i = 0; i < processListFromFile.size(); i++)
|
for (int i = 0; i < processListFromFile.size(); i++)
|
||||||
{
|
{
|
||||||
|
|||||||
11
Process.java
11
Process.java
@ -109,15 +109,20 @@ public class Process {
|
|||||||
remainingTime--;
|
remainingTime--;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
wait(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void wait(int time)
|
||||||
{
|
{
|
||||||
if(waitTime==-1)
|
if(waitTime==-1)
|
||||||
{
|
{
|
||||||
waitTime=1;
|
waitTime=time;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
waitTime++;
|
waitTime+= time;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user