Lab 6 walkthrough — CPU scheduling algorithms: Round Robin, SRTF, Preemptive Priority
Lab 6 walkthrough — CPU scheduling algorithms: Round Robin, SRTF, Preemptive Priority
Recorded by Dr. Krithika L.B. Slot lab06-walkthrough
Theory link: 2.5 Scheduling II
Lab 6 walkthrough — CPU scheduling algorithms: Round Robin, SRTF, Preemptive Priority
Recorded by Dr. Krithika L.B. Slot lab06-walkthrough
Aim. To implement the preemptive Round Robin, SRTF and Preemptive Priority algorithms.
Round Robin keeps a FIFO queue. The process at the front runs for min(quantum, remaining); processes that
arrived during that slice join the queue before the preempted process re-joins at the tail.
SRTF and Preemptive Priority are simulated one time unit at a time: at every tick choose the arrived process with the smallest remaining time (SRTF) or smallest priority number, run it for 1 unit, and repeat. A new arrival with a better value therefore preempts automatically.
/* sched_p.c - preemptive CPU scheduling: Round Robin, SRTF, Preemptive Priority (BACSE106 Lab 6) */
#include <stdio.h>
int main(void)
{
int n, choice, q = 1, i, t = 0, done = 0, last = -1;
int at[20], bt[20], pr[20], rem[20], ct[20];
float twt = 0, ttat = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("P%d arrival burst priority: ", i + 1);
scanf("%d %d %d", &at[i], &bt[i], &pr[i]);
rem[i] = bt[i];
}
printf("1.Round Robin 2.SRTF 3.Preemptive Priority Choice: ");
scanf("%d", &choice);
if (choice == 1) { printf("Time quantum: "); scanf("%d", &q); }
printf("\nGantt chart (time: process):\n");
if (choice == 1) { /* ---------- Round Robin ---------- */
int queue[100], front = 0, rear = 0, inq[20] = {0};
for (i = 0; i < n; i++) /* processes present at time 0 */
if (at[i] <= t) { queue[rear++] = i; inq[i] = 1; }
while (done < n) {
if (front == rear) { /* nobody ready: CPU idle */
t++;
for (i = 0; i < n; i++) if (!inq[i] && at[i] <= t) { queue[rear++] = i; inq[i] = 1; }
continue;
}
int k = queue[front++];
int run = rem[k] < q ? rem[k] : q;
printf("%d: P%d ", t, k + 1);
t += run;
rem[k] -= run;
for (i = 0; i < n; i++) /* arrivals during this slice join first */
if (!inq[i] && at[i] <= t) { queue[rear++] = i; inq[i] = 1; }
if (rem[k] > 0) queue[rear++] = k; /* back to the tail */
else { ct[k] = t; done++; }
}
} else { /* ---------- SRTF / Priority ---------- */
while (done < n) {
int k = -1;
for (i = 0; i < n; i++) {
if (rem[i] == 0 || at[i] > t) continue;
if (k == -1) { k = i; continue; }
if (choice == 2 && rem[i] < rem[k]) k = i;
if (choice == 3 && pr[i] < pr[k]) k = i;
}
if (k == -1) { t++; continue; }
if (k != last) printf("%d: P%d ", t, k + 1); /* print only on a switch */
last = k;
rem[k]--; /* run for one time unit */
t++;
if (rem[k] == 0) { ct[k] = t; done++; }
}
}
printf("%d: end\n\nProcess AT BT PR CT TAT WT\n", t);
for (i = 0; i < n; i++) {
int tat = ct[i] - at[i], wt = tat - bt[i];
ttat += tat;
twt += wt;
printf("P%-7d %-3d %-3d %-3d %-3d %-4d %d\n", i + 1, at[i], bt[i], pr[i], ct[i], tat, wt);
}
printf("Average TAT = %.2f\nAverage WT = %.2f\n", ttat / n, twt / n);
return 0;
}gcc sched_p.c -o sched_p./sched_pGantt chart (time: process):
0: P1 4: P2 7: P3 10: P1 14: P1 18: P1 22: P1 26: P1 30: end
Process AT BT PR CT TAT WT
P1 0 24 1 30 30 6
P2 0 3 1 7 7 4
P3 0 3 1 10 10 7
Average TAT = 15.67
Average WT = 5.67Gantt chart (time: process):
0: P1 1: P2 5: P4 10: P1 17: P3 26: end
Process AT BT PR CT TAT WT
P1 0 8 1 17 17 9
P2 1 4 1 5 4 0
P3 2 9 1 26 24 15
P4 3 5 1 10 7 2
Average TAT = 13.00
Average WT = 6.50Gantt chart (time: process):
0: P1 1: P2 4: P3 5: P1 9: P4 11: P5 15: end
Process AT BT PR CT TAT WT
P1 0 5 3 9 9 4
P2 1 3 1 4 3 0
P3 2 1 2 5 3 2
P4 3 2 4 11 8 6
P5 4 4 5 15 11 7
Average TAT = 6.80
Average WT = 3.80Prepare these for the lab evaluation.
Revealed 0It degenerates to FCFS.
So a newly arriving shorter process can preempt at the exact time it arrives.
No — long processes can starve if short ones keep arriving.
That is the usual convention (arrival at time t is processed before re-queueing at t); state your convention in exams.