CPU scheduling is a fundamental concept in operating systems that determines how processes are allocated processor time. The CPU scheduler makes decisions about which process should run next when multiple processes are competing for CPU resources. Understanding different scheduling algorithms is crucial for system performance optimization and resource management.
What is CPU Scheduling?
CPU scheduling is the process of selecting which process should be executed by the CPU at any given time. When multiple processes are ready to execute, the operating system must decide the order and duration of execution. This decision-making process is handled by the CPU scheduler, which uses various algorithms to optimize system performance.
Key Scheduling Concepts
- Burst Time: The amount of time a process requires for CPU execution
- Arrival Time: The time when a process arrives in the ready queue
- Completion Time: The time when a process finishes execution
- Turnaround Time: Completion time minus arrival time
- Waiting Time: Time spent waiting in the ready queue
- Response Time: Time from arrival until first CPU allocation
First Come First Served (FCFS) Algorithm
FCFS is the simplest CPU scheduling algorithm where processes are executed in the order they arrive in the ready queue. It follows a non-preemptive approach, meaning once a process starts executing, it continues until completion or until it voluntarily releases the CPU.
FCFS Implementation Example
Problem:
Four processes arrive with the following burst times:
| Process | Arrival Time | Burst Time |
|---|---|---|
| P1 | 0 | 5 |
| P2 | 1 | 3 |
| P3 | 2 | 8 |
| P4 | 3 | 6 |
Solution:
| Process | Completion Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 5 | 5 | 0 |
| P2 | 8 | 7 | 4 |
| P3 | 16 | 14 | 6 |
| P4 | 22 | 19 | 13 |
Average Turnaround Time: (5 + 7 + 14 + 19) / 4 = 11.25 units
Average Waiting Time: (0 + 4 + 6 + 13) / 4 = 5.75 units
FCFS Advantages and Disadvantages
Advantages:
- Simple to understand and implement
- No starvation – every process gets executed eventually
- Fair scheduling based on arrival order
- Low overhead for context switching
Disadvantages:
- Convoy effect – short processes wait behind long processes
- High average waiting time
- Not suitable for interactive systems
- CPU utilization may be poor
Shortest Job First (SJF) Algorithm
SJF scheduling selects the process with the shortest burst time for execution next. This algorithm can be implemented in both preemptive and non-preemptive versions. The preemptive version is also known as Shortest Remaining Time First (SRTF).
Non-Preemptive SJF Example
Problem:
Five processes with different arrival and burst times:
| Process | Arrival Time | Burst Time |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 4 | 1 |
| P4 | 5 | 4 |
Execution Order:
P1 (0-7) → P3 (7-8) → P2 (8-12) → P4 (12-16)
| Process | Completion Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 7 | 7 | 0 |
| P2 | 12 | 10 | 6 |
| P3 | 8 | 4 | 3 |
| P4 | 16 | 11 | 7 |
Average Turnaround Time: (7 + 10 + 4 + 11) / 4 = 8 units
Average Waiting Time: (0 + 6 + 3 + 7) / 4 = 4 units
Preemptive SJF (SRTF) vs Non-Preemptive SJF
| Aspect | Non-Preemptive SJF | Preemptive SJF (SRTF) |
|---|---|---|
| Execution | Process runs to completion | Can be interrupted by shorter jobs |
| Context Switching | Minimal | Higher overhead |
| Optimality | Optimal for given arrival order | Optimal average waiting time |
| Implementation | Simpler | More complex |
SJF Advantages and Disadvantages
Advantages:
- Provides minimum average waiting time
- Optimal for batch processing systems
- Efficient CPU utilization
- Better throughput compared to FCFS
Disadvantages:
- Starvation problem for longer processes
- Difficult to predict burst times accurately
- Not suitable for interactive systems
- Requires knowledge of future burst times
Round Robin (RR) Algorithm
Round Robin is a preemptive scheduling algorithm designed specifically for time-sharing systems. Each process is assigned a fixed time slice called a time quantum or time slice. When a process’s time quantum expires, it’s moved to the back of the ready queue, and the next process gets the CPU.
Round Robin Implementation Example
Problem:
Four processes with Time Quantum = 3 units:
| Process | Arrival Time | Burst Time |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
Execution Timeline:
3-6: P2 (remaining: 1)
6-9: P3 (remaining: 6)
9-12: P4 (remaining: 2)
12-15: P1 (remaining: 2)
15-16: P2 (complete)
16-19: P3 (remaining: 3)
19-21: P4 (complete)
21-23: P1 (complete)
23-26: P3 (complete)
| Process | Completion Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 23 | 23 | 15 |
| P2 | 16 | 15 | 11 |
| P3 | 26 | 24 | 15 |
| P4 | 21 | 18 | 13 |
Average Turnaround Time: (23 + 15 + 24 + 18) / 4 = 20 units
Average Waiting Time: (15 + 11 + 15 + 13) / 4 = 13.5 units
Time Quantum Selection
The choice of time quantum is crucial for Round Robin performance:
- Too Small: High context switching overhead, reduced CPU efficiency
- Too Large: Approaches FCFS behavior, loses time-sharing benefits
- Optimal Range: Typically 10-100 milliseconds, should be larger than context switch time
Rule of Thumb: About 80% of processes should complete within one time quantum for optimal performance.
Round Robin Advantages and Disadvantages
Advantages:
- Fair allocation of CPU time to all processes
- No starvation problem
- Excellent for interactive and time-sharing systems
- Good response time for short processes
- Preemptive nature prevents monopolization
Disadvantages:
- Higher average turnaround time than SJF
- Context switching overhead
- Performance depends heavily on time quantum size
- Not optimal for batch processing
Algorithm Comparison and Performance Analysis
Comparative Example:
Same process set with all three algorithms:
| Process | Burst Time |
|---|---|
| P1 | 6 |
| P2 | 8 |
| P3 | 7 |
| P4 | 3 |
| Algorithm | Avg Waiting Time | Avg Turnaround Time | Context Switches |
|---|---|---|---|
| FCFS | 10.75 units | 16.75 units | 3 |
| SJF | 7 units | 13 units | 3 |
| Round Robin (q=4) | 9.25 units | 15.25 units | 7 |
When to Use Each Algorithm
| Algorithm | Best Use Cases | System Type |
|---|---|---|
| FCFS | Batch processing, simple systems, predictable workloads | Embedded systems, batch systems |
| SJF | Minimize average waiting time, known burst times | Batch processing systems |
| Round Robin | Interactive systems, fair resource sharing | Time-sharing, multi-user systems |
Advanced Concepts and Variations
Priority Scheduling Integration
Real operating systems often combine these basic algorithms with priority-based scheduling:
- Priority + FCFS: Higher priority processes scheduled first, FCFS within same priority
- Priority + Round Robin: Different time quanta for different priority levels
- Multi-level Queue: Separate queues for different process types
Adaptive Algorithms
Modern systems use adaptive approaches:
- Exponential Averaging: Predict next CPU burst based on previous bursts
- Feedback Scheduling: Adjust priority based on past behavior
- Lottery Scheduling: Probabilistic fair scheduling
Implementation Considerations
Data Structures
- FCFS: Simple FIFO queue
- SJF: Priority queue or sorted list
- Round Robin: Circular queue with timer mechanism
Performance Metrics
Key Performance Indicators:
- CPU Utilization: Percentage of time CPU is busy
- Throughput: Number of processes completed per unit time
- Response Time: Time from submission to first response
- Fairness: Equal opportunity for all processes
Real-World Applications
Understanding where each algorithm is applied in practice:
- Linux CFS (Completely Fair Scheduler): Enhanced Round Robin with virtual runtime
- Windows Scheduler: Multi-level feedback queue with aging
- Real-time Systems: Rate Monotonic and Earliest Deadline First
- Cloud Computing: Fair queuing and weighted round robin
Conclusion
CPU scheduling algorithms form the foundation of operating system performance and user experience. FCFS provides simplicity but suffers from the convoy effect. SJF offers optimal average waiting time but faces practical challenges with burst time prediction and starvation issues. Round Robin excels in interactive environments by providing fair CPU sharing and good response times.
The choice of scheduling algorithm depends on system requirements, workload characteristics, and performance objectives. Modern operating systems typically employ hybrid approaches, combining multiple algorithms with dynamic adjustments to optimize performance across diverse scenarios. Understanding these fundamental algorithms provides the groundwork for comprehending more complex scheduling strategies used in contemporary computing environments.
As computing continues to evolve with multi-core processors, cloud computing, and real-time requirements, CPU scheduling algorithms continue to adapt and improve, but the core principles established by FCFS, SJF, and Round Robin remain essential knowledge for system designers and developers.








