converted from inded check to id check

This commit is contained in:
Zach S-B 2021-08-28 11:00:33 +10:00
parent b0b108aed6
commit ef07a1b3aa
4 changed files with 58 additions and 95 deletions

View File

@ -103,7 +103,7 @@ public class FCFS {
timeStep++;
input.runForOneTick();
input.runForOneTick(true);
if(input.isDone())
{

View File

@ -105,10 +105,24 @@ public class Process {
//Functions
public void runForOneTick()
public void runForOneTick(boolean isRunning)
{
if(isRunning)
{
remainingTime--;
}
else
{
if(waitTime==-1)
{
waitTime=1;
}
else
{
waitTime++;
}
}
}
public boolean isDone()
{

View File

@ -55,7 +55,6 @@ public class SRT {
{
waitingQueue.add(newProcesses.get(i));
}
newProcesses.clear();
}
@ -63,38 +62,44 @@ public class SRT {
{
System.out.println("SRT:");
int processesFinished = 0;
boolean startNew = true;
int shortestProcess = -1;
int lastRunProcess = -1;
//timeStep++;
Process currentProcess;
Process lastRunProcess = new Process("temp", 0, 0, 0); //temp process for comparing the first run
do
{
checkForArrivals();
shortestProcess = findShortestProcess();
checkForArrivals(); //Check for any new processes since timestep
currentProcess = findShortestProcess(); //get the shortest one out of that
//As long as there is something in the queue
if(waitingQueue.size()>0)
{
if(lastRunProcess!=shortestProcess)
if(!lastRunProcess.getId().equals(currentProcess.getId()))
{
System.out.println("T"+timeStep+": "+waitingQueue.get(shortestProcess).getId());
timeStep = timeStep+disp;
System.out.println("T"+timeStep+": "+currentProcess.getId());
for(int i = 0; i<waitingQueue.size(); i++)
{
waitingQueue.get(i).runForOneTick(false);
}
}
runProcessTick(waitingQueue.get(shortestProcess));
runProcessTick(currentProcess);
if(waitingQueue.get(shortestProcess).isDone())
//convert to all be process driven instead of index driven
if(currentProcess.isDone())
{
waitingQueue.remove(shortestProcess);
waitingQueue.remove(currentProcess);
processesFinished++;
}
lastRunProcess = shortestProcess;
lastRunProcess = currentProcess;
}
}while(processesFinished!=processListFromFile.size());
}
private int findShortestProcess()
private Process findShortestProcess()
{
int listPos = 0;
int remaining = waitingQueue.get(0).getRemainingTime();
@ -108,22 +113,33 @@ public class SRT {
}
}
return listPos;
return waitingQueue.get(listPos);
}
private void runProcessTick(Process input)
{
input.saveStartTime(timeStep);
timeStep++;
input.runForOneTick();
for(int i = 0; i< waitingQueue.size(); i++)
{
if(!input.getId().equals(waitingQueue.get(i).getId()))
{
waitingQueue.get(i).runForOneTick(false);
}
else
{
waitingQueue.get(i).runForOneTick(true);
}
}
timeStep++;
if(input.isDone())
{
input.saveEndTime(timeStep);
input.saveTurnTime();
input.saveWaitTime();
}
}

View File

@ -28,10 +28,10 @@ public class Scheduler {
readFile(fileName); //Read the file
//printFile();
// FCFS fcfs = new FCFS(disp, processList);
//fcfs.runDispatch();
// fcfs.printResults();
//reset();
FCFS fcfs = new FCFS(disp, processList);
fcfs.runDispatch();
fcfs.printResults();
reset();
System.out.println();
@ -178,71 +178,4 @@ public class Scheduler {
System.out.println("End Random.");
}
public void algorithmLTR()
{
// counter: used to track if weve found the winner yet
int counter = 0;
int totalTickets = 0;
int winner = -1;
//count the total number of tickets in play
for (int i = 0; i < processList.size() ; i++)
{
totalTickets += processList.get(i).getTickets();
}
// winner: use some call to a random number generator to
// get a value, between 0 and the total # of tickets
int winnerValue = returnRandomNum(totalTickets);
for (int i = 0; i < processList.size(); i++)
{
counter = processList.get(i).getTickets();
if(counter > winnerValue)
{
winner = i;
break;
}
}
if(winner == -1)
{
System.out.println("No winner");
}
else
{
System.out.println("Winner: " + processList.get(winner).getId());
}
}
public int returnRandomNum(int totalTickets)
{
/*
Friendly neighbourhood Latch 08/08/2021
I figured it out
Keep looping through the list of unfinished processes, adding that process's number of tickets to the counter, until you get to one that is greater than the winning number. If you get to the end of the list, jump back to the start and keep looping through
At the moment it's a disgusting while loop I'm wondering if there's a way I can do that a bit nicer because while loops give me bad vibes but it does the trick
*/
int randomValue = 0;
//Loop back to begining. Probably could remove each consumed random number from the linked list.... but I don't want to right now.
if((randomNumConsumed+1) >= randomNum.size())
{
randomNumConsumed = 0;
}
else
{
randomNumConsumed += 1;
}
randomValue = randomNum.get(randomNumConsumed) % totalTickets;
return randomValue;
}
}