Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Round-robin scheduling

Round-robin scheduling gives ready jobs CPU time in turn, for at most one time quantum each. The C example simulates this policy and calculates waiting time and turnaround time under an explicit arrival-ordering rule.

Version: C23; repository compiler flag, production runtime not attested

Syntax

slice = min(remaining, quantum); turnaround = completion - arrival; waiting = turnaround - burst

Round-robin scheduling (RR) serves a queue of ready jobs. The head job runs for at most a quantum, a positive time slice. If still ready and unfinished, it moves to the tail; completed jobs leave, while blocked jobs wait outside the ready queue. A shorter quantum favors responsiveness but increases switching overhead; RR does not generally minimize average turnaround time.

Arrival ordering needs an explicit rule: append new jobs to the queue and resolve ties deterministically. An arrival during a quantum does not itself interrupt that slice. At quantum boundaries, specify whether new arrivals or the current job's reinsertion are processed first. Here all jobs arrive at time 0, ordered P1, P2, P3; cycling through the array suffices only for this simplification.

Turnaround time is completion - arrival. Waiting time totals time in the ready queue, not just the delay before the first run. For the assumed model without I/O or overhead, it equals turnaround - burst. The example uses quantum = 2 and CPU demands 5, 3, 1; completion times are respectively 9, 8, 5.

This is a deterministic single-CPU simulation in abstract time units, not an OS scheduler configuration or evidence of C thread behavior. Version scope: C23; repository compiler flag, production runtime not attested. Production execution is not attested.

Examples

#include <stdio.h>

int main(void)
{
    const int quantum = 2;
    const int burst[3] = {5, 3, 1};
    int remaining[3] = {5, 3, 1};
    int completion[3] = {0};
    int time = 0;
    int unfinished = 3;

    while (unfinished > 0) {
        for (int i = 0; i < 3; ++i) {
            if (remaining[i] == 0) {
                continue;
            }
            int slice = remaining[i] < quantum
                      ? remaining[i] : quantum;
            printf("P%d [%d,%d)\n", i + 1, time, time + slice);
            time += slice;
            remaining[i] -= slice;
            if (remaining[i] == 0) {
                completion[i] = time;
                --unfinished;
            }
        }
    }

    puts("job waiting turnaround");
    for (int i = 0; i < 3; ++i) {
        int turnaround = completion[i]; /* arrival = 0 */
        int waiting = turnaround - burst[i];
        printf("P%d %d %d\n", i + 1, waiting, turnaround);
    }
    return 0;
}

Result

P1 [0,2)
P2 [2,4)
P3 [4,5)
P1 [5,7)
P2 [7,8)
P1 [8,9)
job waiting turnaround
P1 4 9
P2 5 8
P3 4 5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources