-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe6_sche.c
548 lines (507 loc) · 11.4 KB
/
e6_sche.c
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
// Emmanuel Jojy
// S4 CSE A
// Roll no.: 53
// Scheduling Algorithms
#include <stdio.h>
#include <stdlib.h>
// Represent a process
struct pcb
{
// Process Attributes
int i, a, b, br, s, p, c, t, w;
};
struct pcb *p;
// Represent element of ready queue
struct qnode
{
struct pcb *node;
struct qnode *link;
};
struct qnode *front = NULL, *rear = NULL;
// Represent a Gantt entry
struct gantt
{
int id; // PID
int time; // Start time
};
struct gantt *g;
int gmax = -1;
int algo, tot, tq = 0, flag = 0;
// Add entry to Gantt Chart
// Set id to -1 if IDLE/END
void addGantt(int id, int time)
{
gmax++;
g[gmax].id = id;
g[gmax].time = time;
}
// Auxilliary function for Swap
// Called from sort()
void swap(struct pcb **a, struct pcb **b)
{
struct pcb *temp = *a;
*a = *b;
*b = temp;
}
// Draw horizontal line
// Tab = 8 spaces
void hLine(int tab)
{
int i;
printf("\n");
for (i = 0; i <= tab * 8; i++)
printf("-");
}
// Performs final tabulation
// Common for all algorithms
void tabulate()
{
float tt = 0, wt = 0;
int i, j, flag = 0;
// Gantt Chart
printf("\nGantt Chart\n");
hLine(gmax * 2);
printf("\n|\t");
for (i = 0; i < gmax; i++)
{
if (flag == 1 && g[i].id == -1)
continue;
else if (g[i].id == -1)
{
flag = 1;
printf("--\t|\t");
}
else
{
flag = 0;
printf("P%d\t|\t", g[i].id);
}
}
hLine(gmax * 2);
printf("\n");
for (i = 0; i <= gmax; i++)
{
if (flag == 1 && g[i].id == -1)
continue;
if (g[i].id == -1)
flag = 1;
else
flag = 0;
printf("[%d]\t\t", g[i].time);
}
printf("\n\t ");
// Calculating final TT, WT
for (i = 0; i < tot; i++)
{
p[i].t = p[i].c - p[i].a;
p[i].w = p[i].t - p[i].b;
tt += p[i].t;
wt += p[i].w;
}
// Tabular data
printf("\n\nFINAL TABULATION (TIME QUANTUM = %d)", tq);
hLine(16);
printf("\n|\tID\t|\tAT\t|\tBT\t|\tPR\t||\tST\t|\tCT\t|\tTT\t|\tWT\t|");
hLine(16);
for (i = 0; i < tot; i++)
{
struct pcb t = p[i];
printf("\n|\tP%d\t|\t%d\t|\t%d\t|\t%d\t||\t%d\t|\t%d\t|\t%d\t|\t%d\t|", t.i, t.a, t.b, t.p, t.s, t.c, t.t, t.w);
}
hLine(16);
printf("\n\t\t\t\t\t\t\t\t\t\t\t\t|\t%d\t|\t%d\t|", (int)tt, (int)wt);
hLine(16);
// Average TT, WT
printf("\n\nAverage TT = %f ms", tt / tot);
printf("\nAverage WT = %f ms\n", wt / tot);
// Full forms and assumptions
printf("\n* (ID - PID, AT - Arrival, BT - Burst, PR - Priority, ST - Start, CT - Completion, TT - Turnaround, WT - Wait)\n");
if (algo == 4 || algo == 5)
printf("* Assumption: low number represents high priority (only for priority schedule).");
printf("\n\n");
}
// 0 if incomplete
// 1 if complete
int complete()
{
int i;
for (i = 0; i < tot; i++)
if (p[i].br != 0)
return 0;
flag = 1;
return 1;
}
// Enqueue to ready queue
void push(struct pcb *item)
{
struct qnode *p = malloc(sizeof(struct qnode));
p->node = item;
p->link = NULL;
if (front == NULL)
{
front = p;
rear = p;
}
else
{
rear->link = p;
rear = p;
}
}
// Dequeue from ready queue
struct pcb *pop()
{
if (front == NULL)
return NULL;
struct qnode *temp = front;
struct pcb *item = front->node;
if (front == rear)
{
front = NULL;
rear = NULL;
}
else
{
front = front->link;
}
free(temp);
return item;
}
// Sort Ready Queue
void sort()
{
struct qnode *i, *j;
for (i = front; i != NULL; i = i->link)
{
for (j = front; j->link != NULL; j = j->link)
{
if ((algo == 2 || algo == 3) && j->node->br > j->link->node->br)
swap(&j->node, &j->link->node);
if ((algo == 4 || algo == 5) && j->node->p > j->link->node->p)
swap(&j->node, &j->link->node);
}
}
}
// (1) First Come First Serve
void fcfs()
{
int pulse, i, flag = 0;
struct pcb *current = NULL;
for (pulse = 0; complete() == 0; pulse++)
{
for (i = 0; i < tot; i++)
if (p[i].a == pulse)
push(&p[i]);
if (current != NULL)
{
current->br -= 1;
if (current->br == 0)
{
current->c = pulse;
current = NULL;
}
}
if (current == NULL)
{
current = pop();
if (current == NULL)
addGantt(-1, pulse);
else
{
addGantt(current->i, pulse);
current->s = pulse;
}
}
}
}
// (2) Shortest Job First
void sjf()
{
int pulse, i, flag = 0;
struct pcb *current = NULL;
for (pulse = 0; complete() == 0; pulse++)
{
for (i = 0; i < tot; i++)
if (p[i].a == pulse)
push(&p[i]);
if (current != NULL)
{
current->br -= 1;
if (current->br == 0)
{
current->c = pulse;
current = NULL;
}
}
if (current == NULL)
{
sort();
current = pop();
if (current == NULL)
addGantt(-1, pulse);
else
{
addGantt(current->i, pulse);
current->s = pulse;
}
}
}
}
// (3) Shortest Remaining Job First
void srt()
{
int pulse, i, flag = 0;
struct pcb *current = NULL, *temp = NULL;
for (pulse = 0; complete() == 0; pulse++)
{
for (i = 0; i < tot; i++)
if (p[i].a == pulse)
push(&p[i]);
sort();
if (current != NULL)
{
current->br -= 1;
if (current->br == 0)
{
current->c = pulse;
current = NULL;
}
}
if (front != NULL && current != NULL && current->br > front->node->br)
{
push(current);
current = pop();
addGantt(current->i, pulse);
current->s = pulse;
}
if (current == NULL)
{
sort();
current = pop();
if (current == NULL)
addGantt(-1, pulse);
else
{
addGantt(current->i, pulse);
current->s = pulse;
}
}
}
}
// (4) Priority Scheduling
void pr()
{
int pulse, i, flag = 0;
struct pcb *current = NULL;
for (pulse = 0; complete() == 0; pulse++)
{
for (i = 0; i < tot; i++)
if (p[i].a == pulse)
push(&p[i]);
if (current != NULL)
{
current->br -= 1;
if (current->br == 0)
{
current->c = pulse;
current = NULL;
}
}
if (current == NULL)
{
sort();
current = pop();
if (current == NULL)
addGantt(-1, pulse);
else
{
addGantt(current->i, pulse);
current->s = pulse;
}
}
}
}
// (5) Priority Scheduling Preemptive
void prp()
{
int pulse, i, flag = 0;
struct pcb *current = NULL, *temp = NULL;
for (pulse = 0; complete() == 0; pulse++)
{
for (i = 0; i < tot; i++)
if (p[i].a == pulse)
push(&p[i]);
sort();
if (current != NULL)
{
current->br -= 1;
if (current->br == 0)
{
current->c = pulse;
current = NULL;
}
}
if (front != NULL && current != NULL && current->p > front->node->p)
{
push(current);
current = pop();
addGantt(current->i, pulse);
current->s = pulse;
}
if (current == NULL)
{
sort();
current = pop();
if (current == NULL)
addGantt(-1, pulse);
else
{
addGantt(current->i, pulse);
current->s = pulse;
}
}
}
}
// (6) Round Robin
void rr()
{
int pulse, i, tqq = tq;
struct pcb *current = NULL;
for (pulse = 0; complete() == 0; pulse++)
{
for (i = 0; i < tot; i++)
if (p[i].a == pulse)
push(&p[i]);
if (current != NULL)
{
current->br -= 1;
tqq--;
if (current->br == 0)
{
current->c = pulse;
current = NULL;
}
}
if (tqq == 0 && current != NULL)
{
tqq = tq;
push(current);
current = NULL;
}
if (current == NULL)
{
current = pop();
tqq = tq;
if (current == NULL)
addGantt(-1, pulse);
else
{
addGantt(current->i, pulse);
current->s = pulse;
}
}
}
}
int input()
{
int i;
printf("\nInput Data (Space Separated, Inorder):");
printf("\n(NO - Total Number of Processes, AT - Arrival, BT - Burst, PR - Priority, TQ - Time Quantum)\n\n");
printf("NO: ");
scanf("%d", &tot);
if (tot < 1)
{
printf("\n~ Invalid Number of Processes.\n");
return -1;
}
p = malloc(sizeof(struct pcb) * tot);
// maximum of 100 Gantt entries only.
g = malloc(sizeof(struct gantt) * 100);
printf("AT: ");
for (i = 0; i < tot; i++)
{
scanf("%d", &p[i].a);
if (p[i].a < 0)
{
printf("\n~ Invalid Arrival Time(s).\n");
return -1;
}
// Supply default PID.
p[i].i = i;
p[i].p = 0;
}
printf("BT: ");
for (i = 0; i < tot; i++)
{
scanf("%d", &p[i].b);
if (p[i].b < 1)
{
printf("\n~ Invalid Burst Time(s).\n");
return -1;
}
p[i].br = p[i].b;
}
if (algo == 4 || algo == 5)
{
printf("PR: ");
for (i = 0; i < tot; i++)
scanf("%d", &p[i].p);
}
if (algo == 6)
{
printf("TQ: ");
scanf("%d", &tq);
if (tq < 1)
{
printf("\n~ Invalid Time Slice.\n");
return -1;
}
}
return 0;
}
void main()
{
printf("---- Process Scheduler ---- \n\n");
printf("Scheduling Algorithms: \n");
printf("1. First Come First Serve\n");
printf("2. Shortest Job First\n");
printf("3. Shortest Remaining Job First\n");
printf("4. Priority Scheduling\n");
printf("5. Priority Scheduling (Preemptive)\n");
printf("6. Round Robin\n\n");
printf("Enter Algorithm Choice: ");
scanf("%d", &algo);
if (algo < 1 || algo > 6)
{
printf("\n~ Invalid Choice.\n");
return;
}
if (input() != -1)
{
switch (algo)
{
case 1:
fcfs();
break;
case 2:
sjf();
break;
case 3:
srt();
break;
case 4:
pr();
break;
case 5:
prp();
break;
case 6:
rr();
break;
}
tabulate();
}
free(p);
free(g);
return;
}