mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
converted from inded check to id check
This commit is contained in:
parent
b0b108aed6
commit
ef07a1b3aa
@ -103,7 +103,7 @@ public class FCFS {
|
|||||||
|
|
||||||
timeStep++;
|
timeStep++;
|
||||||
|
|
||||||
input.runForOneTick();
|
input.runForOneTick(true);
|
||||||
|
|
||||||
if(input.isDone())
|
if(input.isDone())
|
||||||
{
|
{
|
||||||
|
|||||||
18
Process.java
18
Process.java
@ -105,9 +105,23 @@ public class Process {
|
|||||||
|
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
public void runForOneTick()
|
public void runForOneTick(boolean isRunning)
|
||||||
{
|
{
|
||||||
remainingTime--;
|
if(isRunning)
|
||||||
|
{
|
||||||
|
remainingTime--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(waitTime==-1)
|
||||||
|
{
|
||||||
|
waitTime=1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
waitTime++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDone()
|
public boolean isDone()
|
||||||
|
|||||||
56
SRT.java
56
SRT.java
@ -55,46 +55,51 @@ public class SRT {
|
|||||||
{
|
{
|
||||||
waitingQueue.add(newProcesses.get(i));
|
waitingQueue.add(newProcesses.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
newProcesses.clear();
|
newProcesses.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runDispatch()
|
public void runDispatch()
|
||||||
{
|
{
|
||||||
System.out.println("SRT:");
|
System.out.println("SRT:");
|
||||||
int processesFinished = 0;
|
int processesFinished = 0;
|
||||||
boolean startNew = true;
|
Process currentProcess;
|
||||||
int shortestProcess = -1;
|
Process lastRunProcess = new Process("temp", 0, 0, 0); //temp process for comparing the first run
|
||||||
int lastRunProcess = -1;
|
|
||||||
//timeStep++;
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
checkForArrivals();
|
checkForArrivals(); //Check for any new processes since timestep
|
||||||
shortestProcess = findShortestProcess();
|
|
||||||
|
currentProcess = findShortestProcess(); //get the shortest one out of that
|
||||||
|
|
||||||
|
//As long as there is something in the queue
|
||||||
if(waitingQueue.size()>0)
|
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;
|
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++;
|
processesFinished++;
|
||||||
}
|
}
|
||||||
lastRunProcess = shortestProcess;
|
|
||||||
|
lastRunProcess = currentProcess;
|
||||||
}
|
}
|
||||||
|
|
||||||
}while(processesFinished!=processListFromFile.size());
|
}while(processesFinished!=processListFromFile.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private int findShortestProcess()
|
private Process findShortestProcess()
|
||||||
{
|
{
|
||||||
int listPos = 0;
|
int listPos = 0;
|
||||||
int remaining = waitingQueue.get(0).getRemainingTime();
|
int remaining = waitingQueue.get(0).getRemainingTime();
|
||||||
@ -108,22 +113,33 @@ public class SRT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return listPos;
|
return waitingQueue.get(listPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runProcessTick(Process input)
|
private void runProcessTick(Process input)
|
||||||
{
|
{
|
||||||
input.saveStartTime(timeStep);
|
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())
|
if(input.isDone())
|
||||||
{
|
{
|
||||||
input.saveEndTime(timeStep);
|
input.saveEndTime(timeStep);
|
||||||
input.saveTurnTime();
|
input.saveTurnTime();
|
||||||
input.saveWaitTime();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,10 +28,10 @@ public class Scheduler {
|
|||||||
readFile(fileName); //Read the file
|
readFile(fileName); //Read the file
|
||||||
//printFile();
|
//printFile();
|
||||||
|
|
||||||
// FCFS fcfs = new FCFS(disp, processList);
|
FCFS fcfs = new FCFS(disp, processList);
|
||||||
//fcfs.runDispatch();
|
fcfs.runDispatch();
|
||||||
// fcfs.printResults();
|
fcfs.printResults();
|
||||||
//reset();
|
reset();
|
||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
@ -177,72 +177,5 @@ public class Scheduler {
|
|||||||
}
|
}
|
||||||
System.out.println("End Random.");
|
System.out.println("End Random.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void algorithmLTR()
|
|
||||||
{
|
|
||||||
// counter: used to track if we’ve 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user