-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPUScheduler.java
242 lines (193 loc) · 9.55 KB
/
CPUScheduler.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
import java.util.ArrayList;
import java.util.List;
public class CPUScheduler implements Runnable {
int timeQuantum;
int currentTime = 0;
int currentTimeSlot = 0;
int pIndex = 0;
int nProcess;
int totalBurstTime = 0;
int totalWaitingTime = 0;
int totalTurnaround = 0;
// For Gantt Chart
List<Integer> procIdSeq, execTimeSeq;
GanttChart g = new GanttChart();
ReadyQueue readyQueue;
public CPUScheduler(ReadyQueue Rqueue, int timeQuantum, List<Integer> procId, List<Integer> execTime) {
this.readyQueue = Rqueue;
this.timeQuantum = timeQuantum;
this.procIdSeq = procId;
this.execTimeSeq = execTime;
}
@Override
public void run() {
try {
waitForJob();
this.nProcess = readyQueue.size();
for (final Job x : this.readyQueue) {
this.totalBurstTime += x.getBurstTime();
}
//System.out.println(this.readyQueue.toString());
//System.out.println(this.timeQuantum);
//System.out.println(this.procIdSeq);
//System.out.println(this.execTimeSeq);
while (!readyQueue.isEmpty()) {
//System.out.println(this.readyQueue.toString());
Thread.sleep(200);//-----------------------------
System.out.println("Current Time: " + this.currentTime + " CPU is running processID: "
+ this.readyQueue.get(pIndex).getProcessID());
// reduce remain burst time of the process by 1 and add time
readyQueue.get(pIndex).setRemainBurstTime(readyQueue.get(pIndex).getRemainBurstTime() - 1);
currentTime++;
currentTimeSlot++;
// the process is completed
if (this.readyQueue.get(pIndex).getRemainBurstTime() == 0) {
procIdSeq.add(Integer.valueOf(this.readyQueue.get(pIndex).getProcessID()));
execTimeSeq.add(Integer.valueOf(this.currentTimeSlot));
this.currentTimeSlot = 0;
// pIndex = (pIndex + 1) % this.readyQueue.size();
this.readyQueue.get(pIndex).setTurnAroundTime(currentTime-readyQueue.get(pIndex).getArrivalTime());
this.readyQueue.get(pIndex).setWaitingTime(
readyQueue.get(pIndex).getTurnAroundTime() - readyQueue.get(pIndex).getBurstTime());
System.out.println(readyQueue.get(pIndex).toString());
readyQueue.get(pIndex).setStatusTrue();
//System.out.println(readyQueue.get(pIndex).getStatusDone());
totalWaitingTime += readyQueue.get(pIndex).getTurnAroundTime() - readyQueue.get(pIndex).getBurstTime();
totalTurnaround += currentTime;
for(int i = 0; i< this.readyQueue.size(); i++){
pIndex = (pIndex + 1) % this.readyQueue.size();
if(readyQueue.get(pIndex).getStatusDone()){ pIndex = (pIndex + 1) % this.readyQueue.size(); break;}
if(!readyQueue.get(pIndex).getStatusDone()){//herer--------------------------------------------
if(readyQueue.get(pIndex).getArrivalTime() <= currentTime){
break;
}
}
}
}
// exceed limit of time quantum
if (this.currentTimeSlot >= this.timeQuantum) {
procIdSeq.add(Integer.valueOf(this.readyQueue.get(pIndex).getProcessID()));
execTimeSeq.add(Integer.valueOf(this.currentTimeSlot));
this.currentTimeSlot = 0;
// finding the next process to run that aready arrived
for(int i = 0; i< this.readyQueue.size(); i++){
pIndex = (pIndex + 1) % this.readyQueue.size();
if(!readyQueue.get(pIndex).getStatusDone()){//herer--------------------------------------------
if(readyQueue.get(pIndex).getArrivalTime() <= currentTime){
break;
}
}
}
// there is no process already arrived add current time until any process arrived
if(readyQueue.get(pIndex).getArrivalTime() > currentTime){
//find the most almost arrive process
pIndex = 0;
for(int i = 0; i< this.readyQueue.size(); i++){
if(readyQueue.get(i).getArrivalTime() <= readyQueue.get(pIndex).getArrivalTime()){
pIndex = i;
}
}
while( readyQueue.get(pIndex).getArrivalTime() > currentTime){
currentTime++;
}
}
}
int allCaseDone = 1;
for(int i = 0; i< this.readyQueue.size(); i++){
if(!readyQueue.get(i).getStatusDone()){allCaseDone = 0;}
}
if(allCaseDone == 1){
System.out.println("Done");
break;
}
}
} catch(InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("**yeeeeeeeeeeeeeeeee!!!");
g.addAllProc(procIdSeq, execTimeSeq);
g.display();
}
private void waitForJob() throws InterruptedException{
synchronized (readyQueue) {
while (readyQueue.isEmpty()) {
// System.out.println("CPU Sched is waiting");
readyQueue.wait();
}
}
}
}
// Last ver by Pollakit
// import java.util.ArrayList;
// public class CPUScheduler implements Runnable {
// int timeQuantum;
// int currentTime = 0;
// int currentTimeSlot = 0;
// int pIndex = 0;
// int nProcess;
// int totalBurstTime = 0;
// int totalWaitingTime = 0;
// int totalTurnaround = 0;
// static ReadyQueue readyQueue;
// public CPUScheduler(final ReadyQueue readyQueue,final int timeQuantum) {
// this.readyQueue = readyQueue;
// this.timeQuantum = timeQuantum;
// }
// @Override
// public void run() {
// this.nProcess = readyQueue.size();
// for (final Job x : this.readyQueue) {
// this.totalBurstTime += x.getBurstTime();
// }
// //System.out.println(readyQueue.toString());
// while (!readyQueue.isEmpty()) {
// System.out.println("Current Time: " + this.currentTime + " CPU is running processID:"
// + this.readyQueue.get(pIndex).getProcessID());
// // reduce remain burst time of the process by 1 and add time
// readyQueue.get(pIndex).setRemainBurstTime(readyQueue.get(pIndex).getRemainBurstTime() - 1);
// currentTime++;
// currentTimeSlot++;
// // the process is completed
// if (this.readyQueue.get(pIndex).getRemainBurstTime() == 0) {
// this.currentTimeSlot = 0;
// this.readyQueue.get(pIndex).setTurnAroundTime(currentTime- readyQueue.get(pIndex).getArrivalTime());
// this.readyQueue.get(pIndex).setWaitingTime(
// readyQueue.get(pIndex).getTurnAroundTime() - readyQueue.get(pIndex).getBurstTime());
// System.out.println(readyQueue.get(pIndex).toString());
// totalWaitingTime += readyQueue.get(pIndex).getTurnAroundTime() - readyQueue.get(pIndex).getBurstTime();
// totalTurnaround += currentTime;
// this.readyQueue.remove(pIndex);
// if (this.readyQueue.size() == 1) {
// this.pIndex = 0;
// }
// }
// // exceed limit of time quantum
// if (this.currentTimeSlot >= this.timeQuantum) {
// this.currentTimeSlot = 0;
// //finding the next process to run that aready arrived
// for(int i = 0; i< this.readyQueue.size(); i++){
// pIndex = (pIndex + 1) % this.readyQueue.size();
// if(readyQueue.get(pIndex).getArrivalTime() <= currentTime){
// break;
// }
// }
// //there is no process already arrived add current time until any process arrived
// if(readyQueue.get(pIndex).getArrivalTime() > currentTime){
// //find the most almost arrive process
// pIndex = 0;
// for(int i = 0; i< this.readyQueue.size(); i++){
// if(readyQueue.get(i).getArrivalTime() <= readyQueue.get(pIndex).getArrivalTime()){
// pIndex = i;
// }
// }
// while( readyQueue.get(pIndex).getArrivalTime() > currentTime){
// currentTime++;
// }
// }
// }
// }
// System.out.println("Average waiting time = " + (float) totalWaitingTime / (float) nProcess);
// System.out.println("Average turn around time = " + (float) totalTurnaround / (float) nProcess);
// System.out.println("Total burst time = " + totalBurstTime);
// }
// }