Overview
Round Robin (RR) is a preemptive CPU scheduling algorithm designed for time-sharing systems. Processes are executed in circular order, each receiving a fixed time slice called a “quantum” before moving to the back of the ready queue.Round Robin is a preemptive algorithm. A running process is interrupted when its quantum expires, even if it hasn’t finished executing.
How It Works
The Round Robin algorithm follows these steps:- Maintain a FIFO ready queue of processes
- New arrivals are added to the end of the queue
- The CPU executes the process at the front for one quantum (or until completion)
- After the quantum expires:
- If the process is complete, remove it
- If not complete, move it to the back of the queue
- Check for new arrivals during execution and add them to the queue
- Repeat until all processes complete
Algorithm Parameters
| Parameter | Type | Description |
|---|---|---|
llegada | Array | Arrival times for each process |
cpu | Array | CPU burst times (total execution needed) |
quantum | Number | Time slice allocated per turn |
The quantum value critically affects performance:
- Too small: High context-switching overhead
- Too large: Degrades to FIFO behavior
- Typical values: 10-100 milliseconds in real systems
Implementation
Here’s the core Round Robin algorithm logic:Example Walkthrough
Let’s simulate Round Robin with quantum = 2:| Process | Arrival Time | CPU Time |
|---|---|---|
| P0 | 0 | 5 |
| P1 | 1 | 3 |
| P2 | 2 | 1 |
Execution Timeline:
Step-by-Step:
-
Time 0-2: P0 executes (quantum=2)
- P0 remaining: 5 → 3
- P1 arrives at time 1, joins queue
- Queue after: [P1, P0]
-
Time 2-4: P1 executes (quantum=2)
- P1 remaining: 3 → 1
- P2 arrives at time 2, joins queue
- Queue after: [P0, P2, P1]
-
Time 4-5: P2 executes (quantum=2, but completes in 1)
- P2 remaining: 1 → 0 (completes!)
- Queue after: [P0, P1]
-
Time 5-6: P0 executes (quantum=2, but only 1 remaining)
- P0 remaining: 3 → 2
- Queue after: [P1, P0]
-
Time 6-7: P1 executes (quantum=2, but only 1 remaining)
- P1 remaining: 1 → 0 (completes!)
- Queue after: [P0]
-
Time 7-9: P0 executes (quantum=2)
- P0 remaining: 2 → 0 (completes!)
- All done!
Results:
| Process | Start | Finish | Waiting | Turnaround |
|---|---|---|---|---|
| P0 | 0 | 9 | 4 | 9 |
| P1 | 2 | 7 | 3 | 6 |
| P2 | 4 | 5 | 2 | 3 |
Key Characteristics
Advantages
- Fair CPU distribution - Every process gets equal time slices
- Good response time - All processes make regular progress
- No starvation - Every process eventually executes
- Ideal for time-sharing - Multiple users get fair access
- Predictable behavior - Upper bound on waiting time
Disadvantages
- Context switching overhead - Frequent switches consume CPU time
- Higher average turnaround time than SJF
- Quantum selection is critical and affects performance
- Not optimal for any specific metric
- Poor for I/O-bound processes that don’t use full quantum
Quantum Selection
Choosing the right quantum value is crucial:Quantum Too Small (e.g., quantum = 1)
- Pros: Excellent response time, very fair
- Cons: Excessive context switching overhead
- Result: CPU spends more time switching than working
Quantum Too Large (e.g., quantum = 100)
- Pros: Minimal context switching
- Cons: Degrades to FIFO behavior
- Result: Long wait times, poor interactivity
Optimal Quantum
- Should be 10-100x the context switch time
- Typically 10-100 milliseconds in real systems
- Should allow 80% of processes to complete in one quantum
Rule of Thumb: Set quantum so that 80% of CPU bursts are shorter than the quantum. This minimizes context switches while maintaining responsiveness.
Performance Characteristics
- Time Complexity: O(n × total_time / quantum) iterations
- Space Complexity: O(n) for process queue
- Response Time: Excellent - O(n × quantum) worst case
- Turnaround Time: Average - depends on quantum
- Throughput: Good for interactive workloads
Variant: Round Robin with Priorities
Some systems combine Round Robin with priorities:Use Cases
Round Robin works best for:
- Time-sharing operating systems (Unix, Windows)
- Interactive multi-user systems
- Web servers handling multiple requests
- Real-time systems with equal-priority tasks
- Any scenario requiring fair resource distribution
Comparison with Other Algorithms
| Metric | FIFO | SJF | Round Robin |
|---|---|---|---|
| Fairness | Fair (arrival) | Unfair | Very Fair |
| Response Time | Poor | Poor | Excellent |
| Avg Waiting Time | Medium | Optimal | Medium |
| Starvation | No | Yes | No |
| Overhead | None | None | High (context switch) |
Try It Out
Use the simulator to experiment with Round Robin:- Try different quantum values (1, 2, 5, 10)
- Observe how small quantum increases fairness but also total time
- Compare with FIFO using the same processes
- Test with many short processes vs. few long processes
Related Algorithms
- FIFO - What RR becomes with infinite quantum
- MLFQ - Multi-level version with varying quantum
- Priority Scheduling - Can be combined with RR per priority level