Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 6: CPU scheduling algorithms: Round Robin, SRTF, Preemptive Priority

Theory link: 2.5 Scheduling II

Experiment 6 of 15 Module 2 2 hours

Video walkthrough

Lab 6 walkthrough — CPU scheduling algorithms: Round Robin, SRTF, Preemptive Priority

Video coming soon

Lab 6 walkthrough — CPU scheduling algorithms: Round Robin, SRTF, Preemptive Priority

Recorded by Dr. Krithika L.B. Slot lab06-walkthrough

Aim

Aim. To implement the preemptive Round Robin, SRTF and Preemptive Priority algorithms.

Explanation

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.

Code

sched_p.c
/* 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;
}

How to run

  1. gcc sched_p.c -o sched_p
  2. ./sched_p
  3. Enter processes (arrival burst priority), the algorithm number, and the quantum for RR.

Sample input & output

Round Robin, q = 4

Input
0 24 1 · 0 3 1 · 0 3 1
Output
Output
Gantt 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.67

SRTF

Input
0 8 1 · 1 4 1 · 2 9 1 · 3 5 1
Output
Output
Gantt 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.50

Preemptive Priority

Input
0 5 3 · 1 3 1 · 2 1 2 · 3 2 4 · 4 4 5
Output
Output
Gantt 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.80

Try it yourself

  1. Count and print the number of context switches for each algorithm.
  2. Run RR with q = 1, 4 and 30 on the same data and compare average WT.
  3. Add response time to the output.
  4. Implement a simple two-queue MLQ: priority 1–2 use RR, others FCFS.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
What happens to RR when the quantum is very large?

It degenerates to FCFS.

Why is SRTF simulated one time unit at a time?

So a newly arriving shorter process can preempt at the exact time it arrives.

Is SRTF fair?

No — long processes can starve if short ones keep arriving.

In RR, why do new arrivals enter the queue before the preempted process?

That is the usual convention (arrival at time t is processed before re-queueing at t); state your convention in exams.