-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPUScheduling.java
208 lines (172 loc) · 5.58 KB
/
CPUScheduling.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
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class CPUScheduling {
private int processNum;
private ArrayList<PCB> PCBs = new ArrayList<PCB>();
private String prcessesChart = "";
public CPUScheduling(int numOfPCBs) {
this.processNum = 0;
this.PCBs = new ArrayList<PCB>(numOfPCBs);
}
public void addPCB(int priority, int arrivalTime, int burstTime) {
PCB pcb = new PCB((processNum + 1), priority, arrivalTime, burstTime);
PCBs.add(pcb);
processNum += 1;
}
public void run() {
ArrayList<PCB> arrivalQueue = new ArrayList<PCB>();
for (PCB p : PCBs) {
p.startTime = -1;
p.terminateTime = 0;
p.waittingTime = 0;
p.remaining = p.cpuBurst;
arrivalQueue.add(p);
}
sortQueueByArrival(arrivalQueue);
ArrayList<PCB> readyQueue = new ArrayList<PCB>();
boolean contextSwitch = false;
boolean cpuState = false;
int cpuClock = 0;
PCB cpuPCB = null;
while (!arrivalQueue.isEmpty() || !readyQueue.isEmpty()
|| cpuState == true) {
while (!arrivalQueue.isEmpty()) {
if (arrivalQueue.get(0).arrivalTime <= cpuClock) {
PCB nextArrival = arrivalQueue.remove(0);
readyQueue.add(nextArrival);
sortQueueByPriority(readyQueue);
} else {
break;
}
}
if (contextSwitch) {
prcessesChart += " | CS";
contextSwitch = false;
if (cpuPCB != null) {
readyQueue.add(cpuPCB);
cpuState = false;
cpuPCB = null;
}
}
if (cpuState == false && !readyQueue.isEmpty()) {
cpuState = true;
cpuPCB = readyQueue.remove(0);
prcessesChart += " | PN" + cpuPCB.id;
if (cpuPCB.startTime < 0)
cpuPCB.startTime = cpuClock;
}
if (cpuState == true && !readyQueue.isEmpty()) {
if (readyQueue.get(0).priority < cpuPCB.priority) {
//readyQueue.add(cpuPCB);
//sortQueueByPriority(readyQueue);
//cpuState = false;
//cpuPCB = null;
contextSwitch = true;
}
}
cpuClock++;
if (cpuPCB != null && !contextSwitch) {
cpuPCB.remaining--;
if (cpuPCB.remaining <= 0) {
int wait = cpuClock - cpuPCB.arrivalTime - cpuPCB.cpuBurst;
cpuPCB.waittingTime = wait;
cpuPCB.terminateTime = cpuClock;
cpuState = false;
cpuPCB = null;
contextSwitch = true;
cpuClock++;
}
}
}
}
private void sortQueueByArrival(ArrayList<PCB> q) {
for (int i = 1; i < q.size(); i++) {
for (int j = 0; j < (q.size() - i); j++) {
if (q.get(j + 1).arrivalTime < q.get(j).arrivalTime) {
PCB p = q.get(j);
q.set(j, q.get(j + 1));
q.set(j + 1, p);
}
}
}
}
private void sortQueueByPriority(ArrayList<PCB> q) {
for (int i = 1; i < q.size(); i++) {
for (int j = 0; j < (q.size() - i); j++) {
if ((q.get(j + 1).priority < q.get(j).priority)
|| ((q.get(j + 1).priority == q.get(j).priority) && (q
.get(j + 1).arrivalTime < q.get(j).arrivalTime))) {
PCB p = q.get(j);
q.set(j, q.get(j + 1));
q.set(j + 1, p);
}
}
}
}
public double getAvgTurnAroundTime() {
double total = 0;
for (PCB p : PCBs)
total += p.getTurnAround();
return total / PCBs.size();
}
public double getAvgWaitTime() {
double total = 0;
for (PCB p : PCBs)
total += p.waittingTime;
return total / PCBs.size();
}
public double getAvgResponseTime() {
double total = 0;
for (PCB p : PCBs)
total += p.getResponseTime();
return total / PCBs.size();
}
public void printReprot() {
System.out.println();
System.out.println("=================================================");
System.out.println("| Detailed information about processes.");
System.out.println("+--------------------------------------");
for (PCB p : PCBs) {
System.out.println(p);
System.out.println();
}
System.out.println("=================================================");
System.out.println("| Scheduling criteria.");
System.out.println("+---------------------");
System.out.println("Avg turnaround time = " + getAvgTurnAroundTime());
System.out.println("Avg waiting time = " + getAvgWaitTime());
System.out.println("Avg response time = " + getAvgResponseTime());
System.out.println("=================================================");
System.out.println("=================================================");
System.out.println("| Processes Chart: " + prcessesChart);
System.out.println("=================================================");
PrintWriter pw = null;
try {
pw = new PrintWriter("Report.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (pw != null) {
pw.println();
pw.println("=================================================");
pw.println("| Detailed information about processes.");
pw.println("+--------------------------------------");
for (PCB p : PCBs) {
pw.println(p);
pw.println();
}
pw.println("=================================================");
pw.println("| Scheduling criteria.");
pw.println("+---------------------");
pw.println("Avg turnaround time = " + getAvgTurnAroundTime());
pw.println("Avg waiting time = " + getAvgWaitTime());
pw.println("Avg response time = " + getAvgResponseTime());
pw.println("=================================================");
pw.println("=================================================");
pw.println("| Processes Chart: " + prcessesChart);
pw.println("=================================================");
pw.close();
}
}
}