Time-sharing systems need every user to get a turn quickly. Round Robin gives each process a short time slice in rotation.

Round Robin (RR)

Round Robin is a preemptive algorithm designed for time-sharing. Each process gets the CPU for a small fixed time quantum (time slice), typically 10 to 100 ms. If it does not finish within the quantum, it is preempted and placed at the end of the ready queue, and the next process runs. If it finishes or blocks earlier, the CPU passes on at once. With n processes and quantum q, no process waits more than (n − 1) × q before its next turn, so response is good and there is no starvation.

Definition: Time quantum. The fixed maximum length of time for which a process may use the CPU in Round Robin scheduling before it is preempted and placed at the end of the ready queue.

Choice of Time Quantum

The performance of RR depends on the quantum. If q is very large, RR becomes FCFS. If q is very small, the overhead of context switches dominates, as seen in the previous topic. A rule of thumb is that about 80 percent of CPU bursts should be shorter than q. Average waiting time under RR is often higher than under SJF, but its response time is much better.

Other Algorithms

Priority scheduling gives the CPU to the process with the highest priority, and can be preemptive or not. It can cause starvation, which is solved by ageing, raising the priority of processes that wait long. Multilevel queue scheduling keeps separate queues, for example for system, interactive and batch processes, each with its own algorithm. Multilevel feedback queue scheduling lets processes move between queues according to their behaviour.

Performance Evaluation of Scheduling Algorithms

Algorithms can be compared in several ways. Deterministic modelling takes a fixed workload and computes the performance of each algorithm, as in the examples here. It is simple and exact but holds only for that workload. Queueing models describe arrivals and service as probability distributions and use formulas such as Little's law, n = λ × W, where n is the average queue length, λ the arrival rate and W the average waiting time. Simulation programs a model of the system and runs it with recorded or random workloads. Implementation codes the algorithm in a real OS and measures it, which is the most accurate but the most costly.

Table 2.6: Comparison of scheduling algorithms

Algorithm Preemptive Starvation Strength
FCFS No No Simple
SJF No Possible Minimum average waiting
SRTF Yes Possible Lower waiting with arrivals
Round Robin Yes No Good response time

Example 2.6: Round Robin with q = 4

Problem: P1 (24 ms), P2 (3 ms), P3 (3 ms) arrive at time 0. Using Round Robin with q = 4 ms, find the average waiting time.

Solution:

  1. Gantt: P1 0–4, P2 4–7, P3 7–10, then P1 runs alone 10–30.
  2. Completion: P1 = 30, P2 = 7, P3 = 10.
  3. Waiting = completion − burst: P1 = 6, P2 = 4, P3 = 7.
  4. Average = 17 / 3 = 5.67 ms.

Result: The average waiting time is 5.67 ms, between SJF (3) and FCFS (17), with fast response for the short jobs.

#include <stdio.h>
int main(void) {
    int burst[] = {24, 3, 3}, rem[3], done[3];
    int n = 3, q = 4, t = 0, left = 3;
    for (int i = 0; i < n; i++) rem[i] = burst[i];
    while (left) {
        for (int i = 0; i < n; i++) {
            if (rem[i] == 0) continue;
            int run = rem[i] < q ? rem[i] : q;
            t += run; rem[i] -= run;
            if (rem[i] == 0) { done[i] = t; left--; }
        }
    }
    double s = 0;
    for (int i = 0; i < n; i++) {
        printf("P%d: completion %d, waiting %d\n",
               i + 1, done[i], done[i] - burst[i]);
        s += done[i] - burst[i];
    }
    printf("average waiting = %.2f ms\n", s / n);
    return 0;
}

Output

P1: completion 30, waiting 6
P2: completion 7, waiting 4
P3: completion 10, waiting 7
average waiting = 5.67 ms

Common mistakes

  • Choosing a very small quantum, which wastes time on context switches.
  • Putting a preempted process at the front instead of the end of the queue.
  • Judging an algorithm only by average waiting time and ignoring response time.

Key takeaways

  • Round Robin gives each process a time quantum in turn.
  • A large quantum makes RR into FCFS; a tiny one causes heavy overhead.
  • RR has no starvation and good response time.
  • Algorithms are evaluated by deterministic modelling, queueing models, simulation and implementation.

Practice questions

  1. What is a time quantum?
  2. What happens if the quantum is very large?
  3. State Little's law.

Answers to the practice questions

  1. The fixed time a process may run in Round Robin before it is preempted.
  2. Round Robin behaves like FCFS.
  3. n = λ × W, the average queue length equals the arrival rate times the average waiting time.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit II