-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgoFCFS.java
177 lines (161 loc) · 5.95 KB
/
AlgoFCFS.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
/*
* AlgoFCFS.java
* Modified : 27/08/2023
*
* This class represents the implementation of the First Come First Serve
* CPU scheduling algorithm
* This file is used in conjunction with Assignment1 for COMP2240.
*/
import java.util.*;
public class AlgoFCFS {
private Queue<myProcess> arrivalQueue; //stores init processes for scheduling
private Queue<myProcess> finishQueue; //stores completed processes
private myProcess currentP; //stores selected running process
private int DIP; //stores global dispatcher time unit
//default constructor
public AlgoFCFS() {
arrivalQueue = new LinkedList<>();
finishQueue = new LinkedList<>();
currentP = null;
DIP = 0;
}
//constructor with parameters
public AlgoFCFS(Queue<myProcess> newArrivalQueue, Queue<myProcess> newReadyQueue
, myProcess newCurrentP, int newDIP) {
arrivalQueue = newArrivalQueue;
finishQueue = newReadyQueue;
currentP = newCurrentP;
DIP = newDIP;
}
//main function that algorithm runs through
public void algoMain() {
System.out.println("FCFS:");
int time = 0; //sets time to 0 to track algorithm lifetime
this.sortAQueue(); //pre-sort arrivalQueue for correct ordering
int aQueueSize = arrivalQueue.size();
while(aQueueSize != finishQueue.size()) {
//sets correct attributes for currentP during round
setCurrent(time, DIP);
//checks if currentP needs to be reselected
checkCurrent(time);
time++;
}
printResults();
}
//sorts finishedQueue by process ID
public void sortQueuebyPID() {
//put in Queue in list to use collections framework
LinkedList<myProcess> ll = new LinkedList<>(finishQueue);
Collections.sort(ll, 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);
}
});
//add all elements back to finishQueue sorted
finishQueue.clear();
finishQueue.addAll(ll);
}
//prints process results of algorithm (TurnAround & Waiting Times)
public void printResults() {
sortQueuebyPID();
System.out.println("Process Turnaround Time Waiting Time");
for (myProcess process : finishQueue) {
int taTime = process.getEndTime() - process.getArrTime();
int wTime = taTime - process.getSrvTime();
System.out.printf("%-9s %-17d %-12d%n", process.getPID(), taTime, wTime);
}
}
//checks that current process has completed it's service time
public boolean processServiced() {
if(currentP.getSrvTime() == currentP.getServedTime()) return true;
return false;
}
//checks current process, adjusting its status accordingly
public void checkCurrent(int time) {
if (currentP == null) return;
//if process service time fin, adjust & add to finish queue
if (processServiced()) {
currentP.setEndTime(time+1);
finishQueue.add(currentP);
currentP = null;
//if not finished, increment service time
} else {
int currentPST = currentP.getServedTime();
currentPST++;
currentP.setServedTime(currentPST);
}
}
//sorts the arrival queue by ascending arrival time
public void sortAQueue() {
Queue<myProcess> sortedQueue = new LinkedList<>();
while (arrivalQueue.size() != 0) { //loops through until old order is 0
Iterator<myProcess> iterWQ = arrivalQueue.iterator();
int currentArrTime = Integer.MAX_VALUE;
//finds shortest arrival time process & adds it to ordered queue
myProcess lowestP = new myProcess();
while(iterWQ.hasNext()) {
myProcess currentP = iterWQ.next();
if (currentArrTime > currentP.getArrTime()) {
currentArrTime = currentP.getArrTime();
lowestP = currentP;
}
}
sortedQueue.add(lowestP);
//Iterates through queue again & removes shortest arrival time process
Iterator<myProcess> rIter = arrivalQueue.iterator();
while(rIter.hasNext()) {
myProcess removalP = rIter.next();
if(removalP == lowestP) { rIter.remove(); }
}
}
//set arrival queue to newly sorted queue
arrivalQueue = sortedQueue;
}
//checks that arrival queue's head can be current process
public boolean AQueueReady(int time) {
if (currentP != null) { return false; }
myProcess headP = arrivalQueue.element();
int headTime = headP.getArrTime();
//if aQueue head arrTime hasn't come, return false
if (headTime > time) { return false; }
return true;
}
//checks if current running process is null replace with aQueue head
public void setCurrent(int time, int DIP) {
if (AQueueReady(time)) {
if (arrivalQueue.element() != null) {
currentP = arrivalQueue.poll();
time += DIP; // add DIP to current time
//outPrint newly selected process to terminal
System.out.println("T"+time+": "+ currentP.getPID());
currentP.setStrtTime(time);
}
}
}
//calculates the average wait time across all processes
public double getAvgWT() {
int processCount = 0;
double totalWT = 0;
for (myProcess process : finishQueue) {
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 : finishQueue) {
totalTT += (process.getEndTime() - process.getArrTime());
processCount++;
}
return totalTT / processCount;
}
}