FCFS working for datafile1

This commit is contained in:
Zach S-B 2021-08-27 12:08:29 +10:00
parent dfd34ddb2c
commit 3fc10bbdd2
4 changed files with 211 additions and 64 deletions

88
FCFS.java Normal file
View File

@ -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<Process> processList = new LinkedList<>();
LinkedList<Process> waitingQueue = new LinkedList<>();
LinkedList<Process> newProcesses = new LinkedList<>();
//Constructors
public FCFS(int dispInput, LinkedList<Process> 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");
}
}
}

View File

@ -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;
}
}

View File

@ -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<Process> processList = new LinkedList<Process>();
LinkedList<Integer> randomNum = new LinkedList<>();
@ -26,57 +26,11 @@ public class Scheduler {
public void run()
{
readFile(fileName); //Read the file
//printFile();
//Test Print
System.out.println("DISP: " + disp + "\n");
FCFS fcfs = new FCFS(disp, processList);
fcfs.runDispatch();
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.");
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;
}
@ -184,20 +138,31 @@ public class Scheduler {
}
}
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;
}
}

62
datafiles/datafilez.txt Normal file
View File

@ -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