-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1.java
196 lines (175 loc) · 7.18 KB
/
A1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* A1.java
* Modified : 27/08/2023
*
* This class represents the implementation main class that all CPU scheduling
* algorithms (FCFS, SRT, FBV, & LTR) run with inputted text file data.
* This file is used in conjunction with Assignment1 for COMP2240.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class A1 {
private LinkedList<myProcess> processList; //process list read from file
private LinkedList<Integer> randLottoNums; //lottoNum list read from file
private int DISP; //Dispatcher value read from file
//default constructor
public A1() {
processList = new LinkedList<>();
randLottoNums = new LinkedList<>();
DISP = 0;
}
public void setDISP(int newDISP) {
DISP = newDISP;
}
//returns class processList
public LinkedList<myProcess> getProcessList() {
return processList;
}
//sets class processList
public void setProcessList(LinkedList<myProcess> newPList) {
processList = newPList;
}
//sets class lottoNums
public void setRandLottoNums(LinkedList<Integer> newLottoNums) {
randLottoNums = newLottoNums;
}
//function used within fileReading(), to specifically scan the lotto nums
public void readRandLottoNums(Scanner fReader) {
LinkedList<Integer> newNList = new LinkedList<>();
while(fReader.hasNext()) {
String randLine = fReader.nextLine().trim();
if (randLine.equals("ENDRANDOM")) {
this.setRandLottoNums(newNList);
break;
} else {
newNList.add(Integer.parseInt(randLine));
}
}
}
//function used within fileReading(), to specifically scan processes
public myProcess ReadProcess(Scanner fReader) {
myProcess fProcess = new myProcess();
while (fReader.hasNext()) {
String line = fReader.nextLine();
if (line.trim().equals("END")) break;
String[] splitLine = line.split(":");
String title = splitLine[0].trim();
String value = splitLine[1].trim();
if(title.equals("ArrTime")) {
int newArrTime = Integer.parseInt(value);
fProcess.setArrTime(newArrTime);
} else if (title.equals("SrvTime")) {
int newSrvTime = Integer.parseInt(value);
fProcess.setSrvTime(newSrvTime);
} else if (title.equals("Tickets")) {
int newTicketCount = Integer.parseInt(value);
fProcess.setTickets(newTicketCount);
}
}
return fProcess;
}
//main function used to read file
//reads dispatcher, random lottery numbers and process list
public void fileReading(Scanner fReader) {
LinkedList<myProcess> newPList = new LinkedList<>();
while (fReader.hasNext()) {
String line = fReader.nextLine().trim();
//If end of file break from loop
if (line.equals("EOF")) {
this.setProcessList(newPList);
break;
//Accounts for random lotto numbers in file
} else if (line.equals("BEGINRANDOM")) {
readRandLottoNums(fReader);
//Accounts for whitespace lines
} else if (line.equals("") || line.equals("BEGIN") || line.equals("END")) {
continue;
//Accounts for Dispatcher & Process file reading
} else {
String[] splitLine = line.split(":");
//Accounts for Dispatcher file reading
if(splitLine[0].trim().equals("DISP")) {
this.setDISP(Integer.parseInt(splitLine[1].trim()));
//Accounts for process file reading
} else if(splitLine[0].trim().equals("PID")) {
String newPID = splitLine[1].trim();
myProcess currentProcess = ReadProcess(fReader);
currentProcess.setPID(newPID);
newPList.add(currentProcess);
}
}
}
}
//used to create deep copies of class process list, copying all process to
//new list. Was needed because couldnt use same list for all algorithms
public static LinkedList<myProcess> copyProcessList(LinkedList<myProcess> ogList) {
LinkedList<myProcess> newList = new LinkedList<>();
for (myProcess process : ogList) {
newList.add(process.deepCopy());
}
return newList;
}
//main method, reads file, runs algorithms, and displays results
public static void main(String args[]) {
//check file exists
if (args.length < 1) {
System.out.println("No file to scan");
return;
}
String fName = args[0];
A1 A1 = new A1(); //create instance of class
try {
File dataFile = new File(fName);
Scanner fReader = new Scanner(dataFile);
A1.fileReading(fReader); //reads file &
fReader.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
int algoDISP = A1.DISP; //Dispatcher value
//data required for First Come First Serve Parameters
LinkedList<myProcess> sListFCFS = copyProcessList(A1.getProcessList());
LinkedList<myProcess> eListFCFS = new LinkedList<>();
//creation and running of FCFS algorithm
AlgoFCFS a1FCFS = new AlgoFCFS(sListFCFS, eListFCFS, null, algoDISP);
a1FCFS.algoMain();
System.out.println("\n");
//data required for Shorest Remaining Time Parameters
LinkedList<myProcess> sListSRT = copyProcessList(A1.getProcessList());
LinkedList<myProcess> eListSRT = new LinkedList<>();
//creation and running of SRT algorithm
AlgoSRT a1SRT = new AlgoSRT(sListSRT, eListSRT, null, algoDISP);
a1SRT.algoMain();
System.out.println("\n");
//data required for FBV Parameters
LinkedList<myProcess> sListFBV = copyProcessList(A1.getProcessList());
LinkedList<myProcess> eListFBV = new LinkedList<>();
LinkedList<myProcess> hpL = new LinkedList<>();
LinkedList<myProcess> mpL = new LinkedList<>();
LinkedList<myProcess> lpL = new LinkedList<>();
//creation and running of FBV algorithm
AlgoFBV a1FBV = new AlgoFBV(sListFBV, eListFBV, hpL, mpL, lpL, algoDISP, 0);
a1FBV.algoMain();
System.out.println("\n");
//data required for LTR Parameters
LinkedList<Integer> lottoNums = A1.randLottoNums;
LinkedList<myProcess> sListLTR = copyProcessList(A1.getProcessList());
LinkedList<myProcess> eListLTR = new LinkedList<>();
//creation and running of FBV algorithm
AlgoLTR a1LTR = new AlgoLTR(lottoNums, 0, algoDISP, sListLTR, eListLTR, null);
a1LTR.algoMain();
System.out.println("\n");
System.out.println("Algorithm Average Turnaround Time Waiting Time");
System.out.printf("%-11s %-25.2f %-15.2f%n", "FCFS", a1FCFS.getAvgTT(), a1FCFS.getAvgWT());
System.out.printf("%-11s %-25.2f %-15.2f%n", "SRT", a1SRT.getAvgTT(), a1SRT.getAvgWT());
System.out.printf("%-11s %-25.2f %-15.2f%n", "FBV", a1FBV.getAvgTT(), a1FBV.getAvgWT());
System.out.printf("%-11s %-25.2f %-15.2f%n", "LTR", a1LTR.getAvgTT(), a1LTR.getAvgWT());
/*
System.out.println("FCFS "+a1FCFS.getAvgTT()+" "+a1FCFS.getAvgWT());
System.out.println("SRT "+a1SRT.getAvgTT()+" "+a1SRT.getAvgWT());
System.out.println("FBV "+a1FBV.getAvgTT()+" "+a1FBV.getAvgWT());
System.out.println("LTR "+a1LTR.getAvgTT()+" "+a1LTR.getAvgWT());
*/
}
}