Lab 5 walkthrough — CPU scheduling algorithms: FCFS, SJF, Priority
Lab 5 walkthrough — CPU scheduling algorithms: FCFS, SJF, Priority
Recorded by Dr. Krithika L.B. Slot lab05-walkthrough
Theory link: 2.4 Scheduling I
Lab 5 walkthrough — CPU scheduling algorithms: FCFS, SJF, Priority
Recorded by Dr. Krithika L.B. Slot lab05-walkthrough
Aim. To implement the non-preemptive FCFS, SJF and Priority scheduling algorithms and compute completion, turnaround and waiting times.
All three algorithms share one loop: at the current time t, look at processes that have arrived and not finished,
pick one by the algorithm's rule, run it to completion (non-preemptive), and advance t by its burst.
Earliest arrival time.
Smallest burst; ties by arrival.
Smallest priority number; ties by arrival.
TAT = CT − AT · WT = TAT − BT
If no process has arrived yet, the CPU is idle and t advances by 1. Check every answer with the simulator in Session 2.4.
/* sched_np.c - non-preemptive CPU scheduling: FCFS, SJF, Priority (BACSE106 Lab 5) */
#include <stdio.h>
int main(void)
{
int n, choice, i, t = 0, done = 0;
int at[20], bt[20], pr[20], ct[20], fin[20] = {0};
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]);
}
printf("1.FCFS 2.SJF 3.Priority Choice: ");
scanf("%d", &choice);
printf("\nGantt chart:\n");
while (done < n) {
int k = -1;
for (i = 0; i < n; i++) {
if (fin[i] || at[i] > t) continue; /* finished or not arrived */
if (k == -1) { k = i; continue; }
if (choice == 1 && at[i] < at[k]) k = i; /* FCFS */
if (choice == 2 && (bt[i] < bt[k] || (bt[i] == bt[k] && at[i] < at[k]))) k = i; /* SJF */
if (choice == 3 && (pr[i] < pr[k] || (pr[i] == pr[k] && at[i] < at[k]))) k = i; /* Priority (low number = high) */
}
if (k == -1) { t++; continue; } /* CPU idle */
printf("| %d P%d %d ", t, k + 1, t + bt[k]);
t += bt[k];
ct[k] = t;
fin[k] = 1;
done++;
}
printf("|\n\nProcess AT BT PR CT TAT WT\n");
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_np.c -o sched_np./sched_npGantt chart:
| 0 P1 24 | 24 P2 27 | 27 P3 30 |
Process AT BT PR CT TAT WT
P1 0 24 1 24 24 0
P2 0 3 1 27 27 24
P3 0 3 1 30 30 27
Average TAT = 27.00
Average WT = 17.00Gantt chart:
| 0 P4 3 | 3 P1 9 | 9 P3 16 | 16 P2 24 |
Process AT BT PR CT TAT WT
P1 0 6 1 9 9 3
P2 0 8 1 24 24 16
P3 0 7 1 16 16 9
P4 0 3 1 3 3 0
Average TAT = 13.00
Average WT = 7.00Gantt chart:
| 0 P2 1 | 1 P5 6 | 6 P1 16 | 16 P3 18 | 18 P4 19 |
Process AT BT PR CT TAT WT
P1 0 10 3 16 16 6
P2 0 1 1 1 1 0
P3 0 2 4 18 18 16
P4 0 1 5 19 19 18
P5 0 5 2 6 6 1
Average TAT = 12.00
Average WT = 8.20Prepare these for the lab evaluation.
Revealed 0SJF (among non-preemptive algorithms, for a given set of processes).
Short processes waiting behind a long one under FCFS.
A low-priority process may never be chosen while higher-priority processes keep arriving; aging fixes it.
To model CPU idle time until the next arrival.