diff --git a/FCFS.java b/FCFS.java new file mode 100644 index 0000000..9e2f1dc --- /dev/null +++ b/FCFS.java @@ -0,0 +1,88 @@ +import java.util.LinkedList; + +public class FCFS { + + private int timeStep = 0; + private int current = 0; + private int disp; + private int timeLastDisp = -1; + + LinkedList processList = new LinkedList<>(); + LinkedList waitingQueue = new LinkedList<>(); + LinkedList newProcesses = new LinkedList<>(); + //Constructors + + public FCFS(int dispInput, LinkedList processListInput) + { + disp = dispInput; + processList = processListInput; + } + + private void checkForArrivals() + { + for(int i = 0; i< processList.size(); i++) + { + if((processList.get(i).getArrive()<=timeStep) && (processList.get(i).getArrive() > timeLastDisp)) + { + + newProcesses.add(processList.get(i)); + } + else + { + + } + } + + for(int i =0; i < newProcesses.size(); i++) + { + waitingQueue.add(newProcesses.get(i)); + } + + newProcesses.clear(); + } + + public void runDispatch() + { + System.out.println("FCFS:"); + do + { + timeStep+=disp; + checkForArrivals(); + timeLastDisp = timeStep; + runProcess(waitingQueue.removeFirst()); + }while(waitingQueue.size()!=0); + + //... + printResults(); + } + + private void runProcess(Process input) + { + + int startTime = timeStep; + int endTime = -1; + + System.out.println("T"+startTime+": "+input.getId()); + + for(int i = 0; i < input.getExecSize(); i++) + { + timeStep++; + } + + endTime = timeStep; + + input.saveTurnTime(endTime-input.getArrive()); + input.saveWaitTime(startTime-input.getArrive()); + } + + public void printResults() + { + System.out.println("\nProcess \tTurnaround Time \tWaiting Time"); + for (int i = 0; i < processList.size(); i++) + { + System.out.print(processList.get(i).getId()+"\t\t"); + System.out.print(processList.get(i).getTurnTime()+"\t\t\t"); + System.out.print(processList.get(i).getWaitTime()+"\n"); + } + } +} \ No newline at end of file diff --git a/Process.java b/Process.java index 682ce08..dd1dc30 100644 --- a/Process.java +++ b/Process.java @@ -5,6 +5,10 @@ public class Process { private int execSize; private int tickets; + //Post Run details + private int turnAroundTime; + private int waitTime; + //Constructors public Process(){ } @@ -32,6 +36,16 @@ public class Process { tickets = ticketsInput; } + public void saveTurnTime(int turnTimeInput) + { + turnAroundTime = turnTimeInput; + } + + public void saveWaitTime(int waitTimeInput) + { + waitTime = waitTimeInput; + } + //Getters public String getId(){ return id; @@ -48,4 +62,14 @@ public class Process { public int getTickets(){ return tickets; } + + public int getTurnTime() + { + return turnAroundTime; + } + + public int getWaitTime() + { + return waitTime; + } } \ No newline at end of file diff --git a/Scheduler.java b/Scheduler.java index f7614f9..3f19729 100644 --- a/Scheduler.java +++ b/Scheduler.java @@ -3,7 +3,7 @@ import java.io.*; public class Scheduler { //Address of File name relative to code. Replace with request for filename? - String fileName = "datafiles/datafile1.txt"; + String fileName = "datafiles/datafile2.txt"; LinkedList processList = new LinkedList(); LinkedList randomNum = new LinkedList<>(); @@ -26,57 +26,11 @@ public class Scheduler { public void run() { readFile(fileName); //Read the file - - //Test Print - System.out.println("DISP: " + disp + "\n"); + //printFile(); - System.out.println("Begin Process List:"); - - for (int i = 0; i < processList.size() ; i++) - { - System.out.println("ID: " + processList.get(i).getId()); - System.out.println("Arrive: " + processList.get(i).getArrive()); - System.out.println("ExecSize: " + processList.get(i).getExecSize()); - System.out.println("Tickets: " + processList.get(i).getTickets()); - System.out.println(); - } - System.out.println("End Process List.\n"); + FCFS fcfs = new FCFS(disp, processList); + fcfs.runDispatch(); - System.out.println("Begin Random:"); - for (int i = 0; i < randomNum.size(); i++) - { - System.out.println(randomNum.get(i)); - } - 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(); } /** @@ -92,7 +46,7 @@ public class Scheduler { //Try and open file. If it works, procede. try { fileReader = new Scanner(dataSet); - System.out.println("File Opened."); + //Progress through each line in the text file while(fileReader.hasNextLine()){ @@ -101,7 +55,7 @@ public class Scheduler { //If the line has "EOF" written, stop reading lines. if(line.equals("EOF")) { - System.out.println("End of File Reached"); + break; } @@ -182,22 +136,33 @@ public class Scheduler { System.out.println("File not found."); //e.printStackTrace(); //Optional, shows more data. } - } - - public void algorithmFCFS() + } + + public void printFile() { + //Test Print + System.out.println("DISP: " + disp + "\n"); + System.out.println("Begin Process List:"); + + for (int i = 0; i < processList.size() ; i++) + { + System.out.println("ID: " + processList.get(i).getId()); + System.out.println("Arrive: " + processList.get(i).getArrive()); + System.out.println("ExecSize: " + processList.get(i).getExecSize()); + System.out.println("Tickets: " + processList.get(i).getTickets()); + System.out.println(); + } + System.out.println("End Process List.\n"); + + System.out.println("Begin Random:"); + for (int i = 0; i < randomNum.size(); i++) + { + System.out.println(randomNum.get(i)); + } + System.out.println("End Random."); } - public void algorithmSRT() - { - - } - - public void algorithmFBV() - { - - } public void algorithmLTR() { @@ -256,6 +221,13 @@ public class Scheduler { 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; @@ -274,4 +246,5 @@ public class Scheduler { return randomValue; } + } \ No newline at end of file diff --git a/datafiles/datafilez.txt b/datafiles/datafilez.txt new file mode 100644 index 0000000..c2fe829 --- /dev/null +++ b/datafiles/datafilez.txt @@ -0,0 +1,62 @@ +BEGIN + +DISP: 1 +END + +ID: p1 +Arrive: 0 +ExecSize: 10 +Tickets: 50 +END + +ID: p2 +Arrive: 0 +ExecSize: 1 +Tickets: 100 +END + +ID: p3 +Arrive: 0 +ExecSize: 2 +Tickets: 125 +END + +ID: p4 +Arrive: 0 +ExecSize: 1 +Tickets: 200 +END + +ID: p5 +Arrive: 0 +ExecSize: 5 +Tickets: 75 +END + + +BEGINRANDOM +844422 +757955 +420572 +258917 +511275 +404934 +783799 +303313 +476597 +583382 +908113 +504687 +281838 +755804 +618369 +250506 +909747 +982786 +810218 +902166 +310147 +729832 +ENDRANDOM + +EOF \ No newline at end of file