-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlgoFBV.java
238 lines (219 loc) · 8.35 KB
/
AlgoFBV.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* AlgoFBV.java
* Modified : 27/08/2023
*
* This class represents the implementation of the Standard Multi-level feedback
* CPU scheduling algorithm, with queue time quants of 2,4,4 (in priority order)
* This file is used in conjunction with Assignment1 for COMP2240.
*/
import java.util.*;
public class AlgoFBV {
private LinkedList<myProcess> startList; //list at beginning of algo
private LinkedList<myProcess> highPriorityList; //top FB queue
private LinkedList<myProcess> midPriorityList; //middle FB queue
private LinkedList<myProcess> lowPriorityList; //bottom FB queue
private LinkedList<myProcess> finishList; //list at end of algo
private myProcess currentP; //holds the currently processing P
private int DIP;
private int time;
//default constructor
public AlgoFBV() {
startList = new LinkedList<>();
highPriorityList = new LinkedList<>();
midPriorityList = new LinkedList<>();
lowPriorityList = new LinkedList<>();
finishList = new LinkedList<>();
DIP = 0;
time = 0;
}
//constructor with parameters
public AlgoFBV(LinkedList<myProcess> newSList, LinkedList<myProcess> newFList,
LinkedList<myProcess> newHPList, LinkedList<myProcess> newMPList,
LinkedList<myProcess> newLPList, int newDIP, int newTime) {
startList = newSList;
finishList = newFList;
highPriorityList = newHPList;
midPriorityList = newMPList;
lowPriorityList = newLPList;
DIP = newDIP;
time = newTime;
}
//main function that algorithm runs through
public void algoMain() {
System.out.println("FBV:");
time = 0; //sets time to 0 to track algorithm lifetime
int initStartSize = startList.size();
while (initStartSize != finishList.size()) {
//check arrival times for start list
time = checkCurrentP(time);
//move arrived processes to high priority list
listArrTimeCheck(time, startList, highPriorityList);
//iterate through priority queues to select a process to run
time = checkPriorityList(0, time);
time = checkPriorityList(1, time);
time = checkPriorityList(2, time);
//check if lowP queue elements have been there longer then 16 secs
//move to highP queue if so
listAgingCheck(lowPriorityList, highPriorityList);
//increment all queue element's waiting time values
incrementListWaiting(highPriorityList);
incrementListWaiting(midPriorityList);
incrementListWaiting(lowPriorityList);
//increment aging values for lowP queue elements
incrementListAging(lowPriorityList);
}
//prints the results of the algorithm
printResults();
}
//sorts finishedList by process ID
public void sortQueuebyPID() {
//put in Queue in list to use collections framework
Collections.sort(finishList, new Comparator<myProcess>() {
@Override
public int compare(myProcess firstP, myProcess secondP) {
String[] firstID = firstP.getPID().split("p");
int idNum1 = Integer.parseInt(firstID[1].trim());
String[] secondID = secondP.getPID().split("p");
int idNum2 = Integer.parseInt(secondID[1].trim());
return Integer.compare(idNum1, idNum2);
}
});
}
//prints the process results of the algorithm (TurnAround & Waiting times)
public void printResults() {
sortQueuebyPID();
System.out.println("Process Turnaround Time Waiting Time");
for (myProcess process : finishList) {
int taTime = process.getEndTime() - process.getArrTime();
int wTime = taTime - process.getSrvTime();
System.out.printf("%-9s %-17d %-12d%n", process.getPID(), taTime, wTime);
}
}
//checks the current running process during algorithm, adjusting its status
//incrementing served service time & quantum time
public int checkCurrentP(int time) {
if (currentP == null) return time; //if current process null return early
//increment served time by 1
int currentST = currentP.getServedTime() + 1;
currentP.setServedTime(currentST);
//increment process time quantum by 1
int currentQT = currentP.getQtTimeSrved() + 1;
currentP.setQtTimeSrved(currentQT);
time = time + 1;
//if service time is complete, remove from current status & add to end List
if (currentP.getSrvTime() == currentST) {
currentP.setEndTime(time);
finishList.add(currentP);
currentP = null;
//if quant time complete, remove from current status & add back to pQueue
} else if (currentQT == currentP.getQtTime()) {
currentP.setQtTimeSrved(0);
//adjust priority queue for required priority level
int priorityLvl = currentP.getPriorityLvl();
if(priorityLvl == 0) midPriorityList.add(currentP);
else lowPriorityList.add(currentP);
currentP = null;
}
return time;
}
//Goes through PriorityList, Reselecting currentP if empty/null
public int checkPriorityList(int level, int time) {
LinkedList<myProcess> currentList = null;
int qtTime = 0;
//set proper attributes for priority lists
if(level == 0) {
currentList = highPriorityList;
qtTime = 2;
} else if (level == 1) {
currentList = midPriorityList;
qtTime = 4;
} else {
currentList = lowPriorityList;
qtTime = 4;
}
if(currentP == null && currentList != null && !currentList.isEmpty()) {
//grab process & set current and remove from list
currentP = currentList.getFirst();
currentList.removeFirst();
//set needed attributes
currentP.setPriorityLvl(level);
currentP.setAgeTime(0);
currentP.setQtTime(qtTime);
time = time + DIP;
//Print to terminal when new process is selected as current
System.out.println("T"+time+": " + currentP.getPID());
}
return time;
}
//move arrived processes to high priority list
public void listArrTimeCheck(int time, LinkedList<myProcess> sList,
LinkedList<myProcess> fList) {
if (sList == null) return;
Iterator<myProcess> iter = sList.iterator();
//iterate through start queue
while (iter.hasNext()) {
myProcess currentFP = iter.next();
int arrTimeFP = currentFP.getArrTime();
if (currentFP.getArrTime() <= time) {
iter.remove();
currentFP.setStrtTime(time);
fList.add(currentFP);
}
}
}
//check if lowP queue elements have been there longer then 16 secs
//move element to highP queue if true
public void listAgingCheck(LinkedList<myProcess> lPList, LinkedList<myProcess> hPList) {
if (lPList == null) return;
Iterator<myProcess> iter = lPList.iterator();
while (iter.hasNext()) {
myProcess currentLP = iter.next();
if(currentLP.getAgeTime() > 16) {
iter.remove();
currentLP.setAgeTime(0);
hPList.add(currentLP);
}
}
}
//increments all of a queue element's waiting time values by 1 second
public void incrementListWaiting(LinkedList<myProcess> cPList) {
if (cPList == null) return;
Iterator<myProcess> iter = cPList.iterator();
while (iter.hasNext()) {
myProcess currentLP = iter.next();
int waitTime = currentLP.getWaitingTime() + 1;
currentLP.setWaitingTime(waitTime);
}
}
//increments age time attributes for priority queue elements
public void incrementListAging(LinkedList<myProcess> lPList) {
if(lPList == null) return;
Iterator<myProcess> iter = lPList.iterator();
while (iter.hasNext()) {
myProcess currentLP = iter.next();
int ageTime = currentLP.getAgeTime() + 1;
currentLP.setAgeTime(ageTime);
}
}
//calculates the average wait time across all processes
public double getAvgWT() {
int processCount = 0;
double totalWT = 0;
for (myProcess process : finishList) {
int taTime = (process.getEndTime() - process.getArrTime());
totalWT += taTime - process.getSrvTime();
processCount++;
}
return totalWT / processCount;
}
//calculates the average turnaround time across all processes
public double getAvgTT() {
int processCount = 0;
double totalTT = 0;
for (myProcess process : finishList) {
totalTT += (process.getEndTime() - process.getArrTime());
processCount++;
}
return totalTT / processCount;
}
}