diff --git a/Scheduler.java b/Scheduler.java index 5f74b39..f7614f9 100644 --- a/Scheduler.java +++ b/Scheduler.java @@ -3,11 +3,11 @@ import java.io.*; public class Scheduler { //Address of File name relative to code. Replace with request for filename? - String fileName = "datafiles/datafile2.txt"; + String fileName = "datafiles/datafile1.txt"; LinkedList processList = new LinkedList(); LinkedList randomNum = new LinkedList<>(); - + int randomNumConsumed = -1; //Counter to track which "random numbers" have been used thus far. //Initialise Dispatcher time variable int disp; @@ -49,6 +49,34 @@ public class Scheduler { } System.out.println("End Random."); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); + algorithmLTR(); } /** @@ -155,4 +183,95 @@ public class Scheduler { //e.printStackTrace(); //Optional, shows more data. } } + + public void algorithmFCFS() + { + + } + + public void algorithmSRT() + { + + } + + public void algorithmFBV() + { + + } + + 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()); + } + + // current: use this to walk through the list of jobs + + //node_t *current = head; + /* + while (current) + { + counter = counter + current->tickets; + if (counter > winner) + { + break; // found the winner + } + + current = current->next; + // 'current' is the winner: schedule it... + } + */ + } + + public int returnRandomNum(int totalTickets) + { + 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; + } + } \ No newline at end of file