CPU Scheduling

Key Terms

TermFormula
Turnaround time (TAT)Completion time − Arrival time
Waiting time (WT)TAT − Burst time
Response timeFirst run time − Arrival time
ThroughputProcesses completed per unit time

Algorithms

AlgorithmPreemptive?Note
FCFSNoSimple; suffers the convoy effect
SJFNoMinimum average WT; needs burst estimates; can starve long jobs
SRTFYesPreemptive SJF
PriorityEitherStarvation solved by aging
Round RobinYesTime quantum; good response time
Multilevel (feedback) queueYesSeparate queues per class; processes move between them

Worked Example: Round Robin (q = 2)

Text
Process  Arrival  Burst
P1       0        5
P2       1        3
P3       2        1

Gantt chart:
| P1 | P2 | P3 | P1 | P2 | P1 |
0    2    4    5    7    8    9

Process  CT  TAT=CT-AT  WT=TAT-BT
P1       9   9          4
P2       8   7          4
P3       5   3          2
Average WT = (4 + 4 + 2) / 3 = 3.33

FCFS in C

C
#include <stdio.h>
int main() {
    int n = 3, bt[] = {24, 3, 3}, wt[3] = {0}, tat[3];
    float awt = 0, atat = 0;
    for (int i = 1; i < n; i++) wt[i] = wt[i - 1] + bt[i - 1];
    for (int i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
        awt += wt[i]; atat += tat[i];
        printf("P%d  BT=%d  WT=%d  TAT=%d\n", i + 1, bt[i], wt[i], tat[i]);
    }
    printf("Avg WT = %.2f, Avg TAT = %.2f\n", awt / n, atat / n);
}
P1 BT=24 WT=0 TAT=24 P2 BT=3 WT=24 TAT=27 P3 BT=3 WT=27 TAT=30 Avg WT = 17.00, Avg TAT = 27.00
Track your progress through the Operating System course