CPU Scheduling Algorithms in Operating System
In our last post, we explored the fundamental concepts of Processes and Threads. Now, it’s time to dive deeper into how the Operating System decides which process gets the CPU next when multiple are ready to run. This critical decision is governed by what we call CPU Scheduling Algorithms.
🔄 What is CPU Scheduling?
The CPU is a limited resource that must be allocated efficiently among competing processes. When more than one process is in the ready queue, the Operating System uses a CPU scheduling algorithm to determine which one to execute first.
⏱ Types of CPU Scheduling Algorithms
Here are the most widely used scheduling techniques in Operating Systems:
- FCFS (First Come First Serve)
- SJF (Shortest Job First)
- Priority Scheduling
- Round Robin
- Multilevel Queue Scheduling
1️⃣ FCFS (First Come First Serve)
In FCFS, the process that arrives first is executed first. It works like a queue in a bank – first in, first out.
Example: Suppose three processes arrive in the order P1 (5s), P2 (10s), P3 (3s). The CPU will handle them in this exact order, without any preemption.
Drawback: If a long process arrives first, shorter ones must wait, leading to the “Convoy Effect”.
2️⃣ SJF (Shortest Job First)
This algorithm selects the process with the smallest burst time (execution time) first. It’s highly efficient for reducing average waiting time.
Example: If P1 = 7s, P2 = 15s, and P3 = 3s, SJF will schedule them as P3 → P1 → P2.
Pros: Gives optimal average waiting time.
Cons: Can lead to starvation of longer jobs if many shorter ones keep coming.
3️⃣ Priority Scheduling
Each process is assigned a priority, and the one with the highest priority runs first. If two processes have the same priority, FCFS is used as a fallback.
Real-life analogy: Emergency rooms treat patients based on severity, not arrival time.
Issue: Like SJF, it may lead to starvation of lower-priority processes.
4️⃣ Round Robin
This is one of the fairest scheduling techniques. Every process gets a small fixed time slice (called a quantum). If it doesn’t finish in that time, it’s sent to the back of the queue.
Example: Used in time-sharing systems where multiple users interact with the system simultaneously.
Pros: Good for fairness and responsiveness
Cons: Choosing the right time quantum is tricky – too small = too many context switches; too large = behaves like FCFS.
5️⃣ Multilevel Queue Scheduling
Processes are grouped into different queues based on their type (foreground, background, system). Each queue may use a different scheduling technique.
Example: Foreground queue (uses Round Robin), Background queue (uses FCFS).
This method separates critical processes from normal ones and improves overall performance.
📊 Comparison Table
Algorithm | Preemptive | Fairness | Best For |
---|---|---|---|
FCFS | No | Low | Simple batch systems |
SJF | No / Yes | Medium | Reducing waiting time |
Priority | Yes | Low | Time-critical tasks |
Round Robin | Yes | High | Time-sharing systems |
Multilevel Queue | Yes | Medium | Hybrid scheduling |
📌 Summary
- CPU Scheduling decides the execution order of processes in the ready queue.
- Algorithms like FCFS, SJF, and Round Robin offer different benefits depending on the system needs.
- The choice of algorithm depends on performance goals like speed, fairness, and priority handling.