Round-robin scheduling and the time quantum

Round robin is a preemptive policy designed for time-sharing. Ready processes wait in a FIFO queue. The scheduler runs the first one for at most a fixed time quantum. If the process has not finished, a timer interrupt preempts it and it goes to the back of the queue.

Python
from collections import deque


def round_robin(bursts, quantum):
    queue = deque(bursts.items())
    time = 0
    finish = {}
    while queue:
        name, remaining = queue.popleft()
        run = min(quantum, remaining)
        time += run
        if remaining > run:
            queue.append((name, remaining - run))
        else:
            finish[name] = time
    return finish


bursts = {"A": 5, "B": 3, "C": 8}
finish = round_robin(bursts, 3)
print(finish)
waits = {name: finish[name] - bursts[name] for name in bursts}
print(waits)
assert sum(finish.values()) > 0
Read the full lesson