C · Course navigation

C: simulating round-robin scheduling

Give each job a fixed time slice in turn and compute the turnaround time of each.

On this page
Before you start

C arrays and loops.

  • Give each job a fixed time slice in turn and compute the turnaround time of each.
  • Predict the sample output and explain the state changes.
  • Complete the practice task and check the boundary cases.

The concept

Round-robin scheduling runs each ready process for at most one quantum and then moves on to the next, cycling until all are done. A process that has not finished waits for its next turn, so no job can monopolize the CPU. The turnaround time of a job is its completion time minus its arrival time.

All jobs arrive at time 0 here. The simulation keeps the remaining burst of each job and a clock. It walks through the jobs in a cycle, runs each unfinished one for min(quantum, remaining), and records when a job's remaining time reaches zero. A smaller quantum is more responsive but costs more context switches.

Code example

C
#include <stdio.h>

int main(void) {
  int remaining[] = {5, 3, 1};
  int finish[] = {0, 0, 0};
  int jobs = 3;
  int quantum = 2;
  int clock = 0;
  int unfinished = jobs;

  while (unfinished > 0) {
    for (int i = 0; i < jobs; i++) {
      if (remaining[i] == 0) {
        continue;
      }
      int run = remaining[i] < quantum ? remaining[i] : quantum;
      clock += run;
      remaining[i] -= run;
      printf("t=%d P%d ran %d\n", clock, i + 1, run);
      if (remaining[i] == 0) {
        finish[i] = clock;
        unfinished--;
      }
    }
  }

  double total = 0;
  for (int i = 0; i < jobs; i++) {
    printf("P%d turnaround %d\n", i + 1, finish[i]);
    total += finish[i];
  }
  printf("average turnaround %.2f\n", total / jobs);
  return 0;
}
Output
t=2 P1 ran 2
t=4 P2 ran 2
t=5 P3 ran 1
t=7 P1 ran 2
t=8 P2 ran 1
t=9 P1 ran 1
P1 turnaround 9
P2 turnaround 8
P3 turnaround 5
average turnaround 7.33

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1#include <stdio.h>
2
3int main(void) {
4 int remaining[] = {5, 3, 1};
5 int finish[] = {0, 0, 0};
6 int jobs = 3;
7 int quantum = 2;
8 int clock = 0;
9 int unfinished = jobs;
10
11 while (unfinished > 0) {
12 for (int i = 0; i < jobs; i++) {
13 if (remaining[i] == 0) {
14 continue;
15 }
16 int run = remaining[i] < quantum ? remaining[i] : quantum;
17 clock += run;
18 remaining[i] -= run;
19 printf("t=%d P%d ran %d\n", clock, i + 1, run);
20 if (remaining[i] == 0) {
21 finish[i] = clock;
22 unfinished--;
23 }
24 }
25 }
26
27 double total = 0;
28 for (int i = 0; i < jobs; i++) {
29 printf("P%d turnaround %d\n", i + 1, finish[i]);
30 total += finish[i];
31 }
32 printf("average turnaround %.2f\n", total / jobs);
33 return 0;
34}

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
A job runs for a full quantum, or less if it needs less.—
—
The clock advances by the time actually used.—
—
P1 and P2 each take two units first, then P3 finishes in one.—
t=2 P1 ran 2
t=4 P2 ran 2
t=5 P3 ran 1
t=7 P1 ran 2
t=8 P2 ran 1
t=9 P1 ran 1
Record the completion time when the remaining burst reaches zero.—
t=2 P1 ran 2
t=4 P2 ran 2
t=5 P3 ran 1
t=7 P1 ran 2
t=8 P2 ran 1
t=9 P1 ran 1
(9 + 8 + 5) / 3 = 7.33.—
t=2 P1 ran 2
t=4 P2 ran 2
t=5 P3 ran 1
t=7 P1 ran 2
t=8 P2 ran 1
t=9 P1 ran 1
P1 turnaround 9
P2 turnaround 8
P3 turnaround 5
average turnaround 7.33

Behind the scenes

How C processes this code

The C compiler produces a native program. Variables occupy memory, and execution enters main.

Cost and common mistakes

Each slice costs O(1). The number of slices is the sum of ceil(burst / quantum) over all jobs.

  • This model ignores the time spent on context switches, which makes tiny quanta look better than they are.
  • With all jobs arriving at time 0, turnaround equals completion time. With later arrivals, subtract the arrival time.
  • A quantum much larger than any burst makes round-robin behave like first-come, first-served.

Try it yourself

How many time slices does this schedule use?

Reveal answer and reasoning

Six slices: P1 uses 3, P2 uses 2, and P3 uses 1.

Further reading