-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.c
More file actions
651 lines (567 loc) · 21.6 KB
/
Copy pathsim.c
File metadata and controls
651 lines (567 loc) · 21.6 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_PROCESSES 100
typedef struct {
int pid;
int arrival_time;
int burst_time;
int priority;
int remaining_time;
int waiting_time;
int turnaround_time;
int completion_time;
int current_queue;
} Process;
void reset_processes(Process p[], int n) {
for (int i = 0; i < n; i++) {
p[i].remaining_time = p[i].burst_time;
p[i].waiting_time = 0;
p[i].turnaround_time = 0;
p[i].completion_time = 0;
p[i].current_queue = 0;
}
}
void print_table(Process p[], int n) {
printf("\n----------------------------------------------------------------------------------\n");
printf("| PID | Priority | Arrival | Burst | Completion | Turnaround | Waiting |\n");
printf("----------------------------------------------------------------------------------\n");
float total_wt = 0, total_tat = 0;
int total_burst = 0;
int max_completion = 0;
for (int i = 0; i < n; i++) {
printf("| %3d | %8d | %7d | %5d | %10d | %10d | %7d |\n",
p[i].pid, p[i].priority, p[i].arrival_time, p[i].burst_time,
p[i].completion_time, p[i].turnaround_time, p[i].waiting_time);
total_wt += p[i].waiting_time;
total_tat += p[i].turnaround_time;
total_burst += p[i].burst_time;
if (p[i].completion_time > max_completion) {
max_completion = p[i].completion_time;
}
}
float cpu_util = 0;
if (max_completion > 0) {
cpu_util = ((float)total_burst / max_completion) * 100.0;
}
printf("----------------------------------------------------------------------------------\n");
printf("Average Waiting Time: %.2f\n", total_wt / n);
printf("Average Turnaround Time: %.2f\n", total_tat / n);
printf("CPU Utilization: %.2f%%\n", cpu_util);
printf("----------------------------------------------------------------------------------\n");
}
void sort_by_arrival(Process p[], int n) {
Process temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].arrival_time > p[j + 1].arrival_time) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
void simulate_fcfs(Process p[], int n) {
printf("\n--- Simulating FCFS ---\n");
reset_processes(p, n);
sort_by_arrival(p, n);
int current_time = 0;
printf("Gantt Chart: ");
for (int i = 0; i < n; i++) {
if (current_time < p[i].arrival_time) {
printf("| IDLE (%d-%d) ", current_time, p[i].arrival_time);
current_time = p[i].arrival_time;
}
int start_time = current_time;
p[i].completion_time = current_time + p[i].burst_time;
p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
current_time = p[i].completion_time;
printf("| P%d (%d-%d) ", p[i].pid, start_time, current_time);
}
printf("|\n");
print_table(p, n);
}
void simulate_round_robin(Process p[], int n, int quantum) {
printf("\n--- Simulating Round Robin (Quantum: %d) ---\n", quantum);
reset_processes(p, n);
sort_by_arrival(p, n);
int current_time = 0;
int completed = 0;
printf("Gantt Chart: ");
while (completed < n) {
bool idle = true;
for (int i = 0; i < n; i++) {
if (p[i].remaining_time > 0 && p[i].arrival_time <= current_time) {
idle = false;
int start_exec = current_time;
if (p[i].remaining_time > quantum) {
current_time += quantum;
p[i].remaining_time -= quantum;
} else {
current_time += p[i].remaining_time;
p[i].waiting_time = current_time - p[i].burst_time - p[i].arrival_time;
p[i].remaining_time = 0;
p[i].completion_time = current_time;
p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
completed++;
}
printf("| P%d (%d-%d) ", p[i].pid, start_exec, current_time);
}
}
if (idle && completed < n) {
int next_arrival_time = 999999;
bool found_next = false;
//daca suntem idle cautam care este urmatorul proces care va sosi
for (int k = 0; k < n; k++) {
if (p[k].remaining_time > 0 && p[k].arrival_time > current_time) {
if (p[k].arrival_time < next_arrival_time) {
next_arrival_time = p[k].arrival_time;
found_next = true;
}
}
}
if (found_next) {
printf("| IDLE (%d-%d) ", current_time, next_arrival_time);
current_time = next_arrival_time;
} else {
// n-ar trebui sa ajunga aici
current_time++;
}
}
}
printf("|\n");
print_table(p, n);
}
void simulate_priority(Process p[], int n) {
printf("\n--- Simulating Priority (Non-Preemptive) ---\n");
reset_processes(p, n);
int current_time = 0;
int completed = 0;
printf("Gantt Chart: ");
while (completed < n) {
int idx = -1;
int highest_priority = 9999;
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0) {
if (p[i].priority < highest_priority) {
highest_priority = p[i].priority;
idx = i;
}
}
}
if (idx != -1) {
int start_exec = current_time;
current_time += p[idx].burst_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
printf("| P%d (%d-%d) ", p[idx].pid, start_exec, current_time);
} else {
int next_arrival_time = 999999;
bool found_next = false;
//daca suntem idle cautam care este urmatorul proces care va sosi
for (int k = 0; k < n; k++) {
if (p[k].remaining_time > 0 && p[k].arrival_time > current_time) {
if (p[k].arrival_time < next_arrival_time) {
next_arrival_time = p[k].arrival_time;
found_next = true;
}
}
}
if (found_next) {
printf("| IDLE (%d-%d) ", current_time, next_arrival_time);
current_time = next_arrival_time;
} else {
// n-ar trebui sa ajunga aici
current_time++;
}
}
}
printf("|\n");
print_table(p, n);
}
void simulate_sjf(Process p[], int n) {
printf("\n--- Simulating SJF (Non-Preemptive) ---\n");
reset_processes(p, n);
int current_time = 0;
int completed = 0;
printf("Gantt Chart: ");
while (completed < n) {
int idx = -1;
int min_burst = 99999;
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0) {
if (p[i].burst_time < min_burst) {
min_burst = p[i].burst_time;
idx = i;
}
}
}
if (idx != -1) {
int start_exec = current_time;
current_time += p[idx].burst_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
printf("| P%d (%d-%d) ", p[idx].pid, start_exec, current_time);
} else {
int next_arrival_time = 999999;
bool found_next = false;
//daca suntem idle cautam care este urmatorul proces care va sosi
for (int k = 0; k < n; k++) {
if (p[k].remaining_time > 0 && p[k].arrival_time > current_time) {
if (p[k].arrival_time < next_arrival_time) {
next_arrival_time = p[k].arrival_time;
found_next = true;
}
}
}
if (found_next) {
printf("| IDLE (%d-%d) ", current_time, next_arrival_time);
current_time = next_arrival_time;
} else {
// n-ar trebui sa ajunga aici
current_time++;
}
}
}
printf("|\n");
print_table(p, n);
}
void simulate_srtf(Process p[], int n) {
printf("\n--- Simulating SRTF (Preemptive SJF) ---\n");
reset_processes(p, n);
int current_time = 0;
int completed = 0;
int shortest = -1;
int min_rem_time = 99999;
bool check = false;
int last_pid = -1;
int start_block = 0;
printf("Gantt Chart: ");
while (completed < n) {
shortest = -1;
min_rem_time = 99999;
check = false;
for (int i = 0; i < n; i++) {
if ((p[i].arrival_time <= current_time) && (p[i].remaining_time > 0)) {
if (p[i].remaining_time < min_rem_time) {
min_rem_time = p[i].remaining_time;
shortest = i;
check = true;
}
}
}
if (!check) { // IDLE
if (last_pid != -1) {
printf("| P%d (%d-%d) ", last_pid, start_block, current_time);
last_pid = -1;
}
int next_arrival = 999999;
bool found_next = false;
for(int k=0; k<n; k++) {
if(p[k].remaining_time > 0 && p[k].arrival_time > current_time) {
if(p[k].arrival_time < next_arrival) {
next_arrival = p[k].arrival_time;
found_next = true;
}
}
}
if (found_next) {
printf("| IDLE (%d-%d) ", current_time, next_arrival);
current_time = next_arrival;
start_block = current_time;
} else {
current_time++;
start_block = current_time;
}
continue;
}
if (last_pid != -1 && p[shortest].pid != last_pid) {
printf("| P%d (%d-%d) ", last_pid, start_block, current_time);
start_block = current_time;
} else if (last_pid == -1) {
start_block = current_time;
}
last_pid = p[shortest].pid;
p[shortest].remaining_time--;
current_time++;
if (p[shortest].remaining_time == 0) {
completed++;
p[shortest].completion_time = current_time;
p[shortest].turnaround_time = p[shortest].completion_time - p[shortest].arrival_time;
p[shortest].waiting_time = p[shortest].turnaround_time - p[shortest].burst_time;
if (p[shortest].waiting_time < 0) p[shortest].waiting_time = 0;
printf("| P%d (%d-%d) ", last_pid, start_block, current_time);
last_pid = -1; // reset
start_block = current_time;
}
}
printf("|\n");
print_table(p, n);
}
void simulate_mlq(Process p[], int n, int quantum_high) {
printf("\n--- Simulating Multilevel Queue (System=RR, User=FCFS) ---\n");
reset_processes(p, n);
sort_by_arrival(p, n);
int current_time = 0;
int completed = 0;
printf("Gantt Chart: ");
while (completed < n) {
int idx = -1;
int selected_queue = -1;
bool found = false;
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0 && p[i].priority <= 5) {
idx = i;
selected_queue = 1;
found = true;
break;
}
}
if (!found) {
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0 && p[i].priority > 5) {
idx = i;
selected_queue = 2;
found = true;
break;
}
}
}
if (found) {
int start_exec = current_time;
if (selected_queue == 1) {
if (p[idx].remaining_time > quantum_high) {
current_time += quantum_high;
p[idx].remaining_time -= quantum_high;
Process temp = p[idx];
int insert_pos = idx;
while(insert_pos < n-1 && p[insert_pos+1].arrival_time <= current_time) {
p[insert_pos] = p[insert_pos+1];
insert_pos++;
}
p[insert_pos] = temp;
idx = insert_pos;
} else {
current_time += p[idx].remaining_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
}
} else {
current_time += p[idx].remaining_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
}
printf("| Q%d:P%d (%d-%d) ", selected_queue, p[idx].pid, start_exec, current_time);
} else {
int next_arrival = 999999;
bool found_next = false;
for(int k=0; k<n; k++) {
if(p[k].remaining_time > 0 && p[k].arrival_time > current_time) {
if(p[k].arrival_time < next_arrival) {
next_arrival = p[k].arrival_time;
found_next = true;
}
}
}
if(found_next) {
printf("| IDLE (%d-%d) ", current_time, next_arrival);
current_time = next_arrival;
} else {
current_time++;
}
}
}
printf("|\n");
print_table(p, n);
}
void simulate_mlfq(Process p[], int n) {
printf("\n--- Simulating MLFQ (Q0=RR4, Q1=RR8, Q2=FCFS) ---\n");
reset_processes(p, n);
sort_by_arrival(p, n);
for(int i=0; i<n; i++) p[i].current_queue = 0;
int current_time = 0;
int completed = 0;
int q0_quantum = 4;
int q1_quantum = 8;
printf("Gantt Chart: ");
while (completed < n) {
int idx = -1;
int queue_level = -1;
bool found = false;
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0 && p[i].current_queue == 0) {
idx = i;
queue_level = 0;
found = true;
break;
}
}
if (!found) {
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0 && p[i].current_queue == 1) {
idx = i;
queue_level = 1;
found = true;
break;
}
}
}
if (!found) {
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0 && p[i].current_queue == 2) {
idx = i;
queue_level = 2;
found = true;
break;
}
}
}
if (found) {
int start_exec = current_time;
if (queue_level == 0) {
if (p[idx].remaining_time > q0_quantum) {
current_time += q0_quantum;
p[idx].remaining_time -= q0_quantum;
p[idx].current_queue = 1;
Process temp = p[idx];
int insert_pos = idx;
while(insert_pos < n-1 && p[insert_pos+1].arrival_time <= current_time) {
p[insert_pos] = p[insert_pos+1];
insert_pos++;
}
p[insert_pos] = temp;
idx = insert_pos;
} else {
current_time += p[idx].remaining_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
}
}
else if (queue_level == 1) {
if (p[idx].remaining_time > q1_quantum) {
current_time += q1_quantum;
p[idx].remaining_time -= q1_quantum;
p[idx].current_queue = 2;
Process temp = p[idx];
int insert_pos = idx;
while(insert_pos < n-1 && p[insert_pos+1].arrival_time <= current_time) {
p[insert_pos] = p[insert_pos+1];
insert_pos++;
}
p[insert_pos] = temp;
idx = insert_pos;
} else {
current_time += p[idx].remaining_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
}
}
else {
current_time += p[idx].remaining_time;
p[idx].remaining_time = 0;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;
p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;
completed++;
}
printf("| Q%d:P%d (%d-%d) ", queue_level, p[idx].pid, start_exec, current_time);
} else {
int next_arrival = 999999;
bool future_process = false;
for(int k=0; k<n; k++) {
if(p[k].remaining_time > 0 && p[k].arrival_time > current_time) {
if(p[k].arrival_time < next_arrival) {
next_arrival = p[k].arrival_time;
future_process = true;
}
}
}
if(future_process) {
printf("| IDLE (%d-%d) ", current_time, next_arrival);
current_time = next_arrival;
} else {
current_time++;
}
}
}
printf("|\n");
print_table(p, n);
}
int main() {
Process processes[MAX_PROCESSES];
int n, quantum, choice;
printf("Enter number of processes: ");
if (scanf("%d", &n) != 1) return 1;
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
printf("Process %d [Arrival, Burst, Priority]: ", i + 1);
fflush(stdout);
if (scanf("%d %d %d", &processes[i].arrival_time, &processes[i].burst_time, &processes[i].priority) != 3) return 1;
processes[i].remaining_time = processes[i].burst_time;
}
while (1) {
printf("\nSCHEDULER SIMULATOR\n");
printf("1. FCFS\n");
printf("2. Round Robin\n");
printf("3. Priority (Non-Preemptive)\n");
printf("4. SJF (Shortest Job First - Non-Preemptive)\n");
printf("5. SRTF (Shortest Remaining Time First - Preemptive)\n");
printf("6. Multilevel Queue (System=RR, User=FCFS)\n");
printf("7. Multilevel Feedback Queue (Q0-Q2)\n");
printf("8. Exit\n");
printf("Choice: ");
if (scanf("%d", &choice) != 1) break;
switch (choice) {
case 1:
simulate_fcfs(processes, n);
break;
case 2:
printf("Enter Time Quantum: ");
scanf("%d", &quantum);
simulate_round_robin(processes, n, quantum);
break;
case 3:
simulate_priority(processes, n);
break;
case 4:
simulate_sjf(processes, n);
break;
case 5:
simulate_srtf(processes, n);
break;
case 6:
printf("Enter Time Quantum for System Queue: ");
scanf("%d", &quantum);
simulate_mlq(processes, n, quantum);
break;
case 7:
simulate_mlfq(processes, n);
break;
case 8:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}