-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3.cpp
More file actions
320 lines (258 loc) · 7.99 KB
/
Assignment3.cpp
File metadata and controls
320 lines (258 loc) · 7.99 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Title: CPU Scheduling Algorithms (FCFS, SJF, Priority, Round Robin)
Description: Menu driven simple implementation with Gantt charts.
Time Complexity: Depends on algorithm (O(n²) worst)
Space Complexity: O(n)
*/
#include <iostream>
using namespace std;
struct Process {
int pid;
int at;
int bt;
int priority;
int rt; // Remaining time (RR)
};
// ------------------------------------------------------------
// Utility : Print Gantt Chart
// ------------------------------------------------------------
void printGantt(int gantt[], int times[], int len) {
cout << "\nGantt Chart:\n ";
for (int i = 0; i < len; i++) {
cout << "| P" << gantt[i] << " ";
}
cout << "|\n";
cout << times[0];
for (int i = 1; i <= len; i++) {
cout << " " << times[i];
}
cout << "\n";
}
// ------------------------------------------------------------
// FCFS
// ------------------------------------------------------------
void FCFS(Process p[], int n) {
// sort by arrival time
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (p[j].at > p[j + 1].at) {
Process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
int wt[20], tat[20], ct[20];
int gantt[20], times[21];
ct[0] = p[0].at + p[0].bt;
tat[0] = ct[0] - p[0].at;
wt[0] = tat[0] - p[0].bt;
for (int i = 1; i < n; i++) {
if (ct[i - 1] < p[i].at)
ct[i] = p[i].at + p[i].bt;
else
ct[i] = ct[i - 1] + p[i].bt;
tat[i] = ct[i] - p[i].at;
wt[i] = tat[i] - p[i].bt;
}
float totalWT = 0, totalTAT = 0;
times[0] = p[0].at;
for (int i = 0; i < n; i++) {
gantt[i] = p[i].pid;
times[i + 1] = ct[i];
totalWT += wt[i];
totalTAT += tat[i];
}
cout << "\n--- FCFS Scheduling ---\n";
cout << "PID\tAT\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].pid << "\t" << p[i].at << "\t" << p[i].bt
<< "\t" << wt[i] << "\t" << tat[i] << "\n";
}
cout << "Average WT = " << totalWT / n << "\n";
cout << "Average TAT = " << totalTAT / n << "\n";
printGantt(gantt, times, n);
}
// ------------------------------------------------------------
// SJF (non-preemptive)
// ------------------------------------------------------------
void SJF(Process p[], int n) {
bool done[20];
int wt[20], tat[20];
int gantt[20], times[21];
int completed = 0, t = 0, gi = 0;
for (int i = 0; i < n; i++) done[i] = false;
times[0] = 0;
while (completed < n) {
int idx = -1, minBT = 99999;
for (int i = 0; i < n; i++) {
if (!done[i] && p[i].at <= t && p[i].bt < minBT) {
minBT = p[i].bt;
idx = i;
}
}
if (idx == -1) {
t++;
continue;
}
gantt[gi] = p[idx].pid;
t += p[idx].bt;
times[gi + 1] = t;
tat[idx] = t - p[idx].at;
wt[idx] = tat[idx] - p[idx].bt;
done[idx] = true;
completed++;
gi++;
}
float totalWT = 0, totalTAT = 0;
cout << "\n--- SJF Scheduling ---\n";
cout << "PID\tAT\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].pid << "\t" << p[i].at << "\t" << p[i].bt
<< "\t" << wt[i] << "\t" << tat[i] << "\n";
totalWT += wt[i];
totalTAT += tat[i];
}
cout << "Average WT = " << totalWT / n << "\n";
cout << "Average TAT = " << totalTAT / n << "\n";
printGantt(gantt, times, completed);
}
// ------------------------------------------------------------
// Priority Scheduling (Non-preemptive)
// ------------------------------------------------------------
void Priority(Process p[], int n) {
bool done[20];
int wt[20], tat[20];
int gantt[20], times[21];
int completed = 0, t = 0, gi = 0;
for (int i = 0; i < n; i++) done[i] = false;
times[0] = 0;
while (completed < n) {
int idx = -1, minP = 99999;
for (int i = 0; i < n; i++) {
if (!done[i] && p[i].at <= t && p[i].priority < minP) {
minP = p[i].priority;
idx = i;
}
}
if (idx == -1) {
t++;
continue;
}
gantt[gi] = p[idx].pid;
t += p[idx].bt;
times[gi + 1] = t;
tat[idx] = t - p[idx].at;
wt[idx] = tat[idx] - p[idx].bt;
done[idx] = true;
completed++;
gi++;
}
float totalWT = 0, totalTAT = 0;
cout << "\n--- Priority Scheduling ---\n";
cout << "PID\tAT\tBT\tPR\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].pid << "\t" << p[i].at << "\t" << p[i].bt
<< "\t" << p[i].priority << "\t" << wt[i] << "\t" << tat[i] << "\n";
totalWT += wt[i];
totalTAT += tat[i];
}
cout << "Average WT = " << totalWT / n << "\n";
cout << "Average TAT = " << totalTAT / n << "\n";
printGantt(gantt, times, completed);
}
// ------------------------------------------------------------
// Round Robin
// ------------------------------------------------------------
void RoundRobin(Process p[], int n, int quantum) {
int wt[20], tat[20];
int gantt[200], times[201];
int g = 0;
int t = 0;
times[0] = 0;
for (int i = 0; i < n; i++) {
p[i].rt = p[i].bt;
wt[i] = tat[i] = 0;
}
bool done;
do {
done = true;
for (int i = 0; i < n; i++) {
if (p[i].rt > 0) {
done = false;
gantt[g] = p[i].pid;
if (p[i].rt > quantum) {
t += quantum;
p[i].rt -= quantum;
} else {
t += p[i].rt;
wt[i] = t - p[i].bt;
p[i].rt = 0;
}
g++;
times[g] = t;
}
}
} while (!done);
for (int i = 0; i < n; i++)
tat[i] = wt[i] + p[i].bt;
float totalWT = 0, totalTAT = 0;
cout << "\n--- Round Robin Scheduling ---\n";
cout << "PID\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].pid << "\t" << p[i].bt << "\t"
<< wt[i] << "\t" << tat[i] << "\n";
totalWT += wt[i];
totalTAT += tat[i];
}
cout << "Average WT = " << totalWT / n << "\n";
cout << "Average TAT = " << totalTAT / n << "\n";
printGantt(gantt, times, g);
}
// ------------------------------------------------------------
// MAIN MENU
// ------------------------------------------------------------
int main() {
int n;
cout << "Enter number of processes (max 20): ";
cin >> n;
Process p[20], backup[20];
for (int i = 0; i < n; i++) {
p[i].pid = i + 1;
cout << "\nEnter Arrival Time for P" << i + 1 << ": ";
cin >> p[i].at;
cout << "Enter Burst Time for P" << i + 1 << ": ";
cin >> p[i].bt;
cout << "Enter Priority for P" << i + 1 << ": ";
cin >> p[i].priority;
backup[i] = p[i];
}
int choice, quantum;
do {
cout << "\n\nCPU Scheduling Menu\n";
cout << "1. FCFS\n";
cout << "2. SJF\n";
cout << "3. Priority\n";
cout << "4. Round Robin\n";
cout << "5. Exit\n";
cout << "Enter choice: ";
cin >> choice;
// Reset processes before each algo
for (int i = 0; i < n; i++)
p[i] = backup[i];
switch (choice) {
case 1: FCFS(p, n); break;
case 2: SJF(p, n); break;
case 3: Priority(p, n); break;
case 4:
cout << "Enter Time Quantum: ";
cin >> quantum;
RoundRobin(p, n, quantum);
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 5);
return 0;
}