What is CPU Scheduling?
Learn what CPU scheduling is — FCFS, Round Robin, priority scheduling, and starvation — with examples and OS interview questions answered.
Expected Interview Answer
CPU scheduling is the OS mechanism that decides which ready process or thread gets to run on the CPU next, aiming to balance throughput, fairness, and responsiveness across competing tasks.
Whenever the CPU becomes idle, a running process blocks, or a timer interrupt fires, the scheduler selects the next task from the ready queue according to a scheduling algorithm — examples include First-Come-First-Served (simple but can cause long waits), Shortest-Job-First (optimal average wait time but needs future knowledge), Round Robin (time-sliced and fair, good for interactive systems), and Priority Scheduling (favors important tasks but risks starving low-priority ones, usually mitigated with aging). Scheduling can be preemptive, where a running task can be interrupted mid-execution, or non-preemptive, where a task runs to completion or until it blocks voluntarily. The scheduler is evaluated on metrics like CPU utilization, throughput, turnaround time, waiting time, and response time, and different algorithms trade these off differently depending on whether the workload is batch, interactive, or real-time.
- Maximizes CPU utilization by keeping it busy with ready work
- Balances fairness so no process is starved indefinitely
- Tunable for interactive responsiveness vs batch throughput
- Foundation for understanding real-time and multi-core scheduling
AI Mentor Explanation
CPU scheduling is like a team management deciding which batter comes in next after a wicket falls: First-Come-First-Served would send in whoever is next on the card regardless of form, while Round Robin gives each batter a fixed number of overs before rotating to the next, ensuring everyone gets a fair turn at the crease. Priority scheduling would send the in-form batter in first, risking that lower-order players never get to bat if not managed carefully (starvation).
Step-by-Step Explanation
Step 1
Task enters ready queue
A process becomes runnable and is placed in the scheduler’s ready queue.
Step 2
Scheduling event
A timer interrupt, blocking call, or CPU idle event triggers the scheduler to run.
Step 3
Algorithm selects next task
The scheduler picks a task using its policy — FCFS, SJF, Round Robin, or priority-based.
Step 4
Dispatch
The dispatcher performs a context switch to hand the CPU to the selected task.
What Interviewer Expects
- Clear explanation of what CPU scheduling decides and why it is needed
- Ability to name and contrast at least two scheduling algorithms
- Distinction between preemptive and non-preemptive scheduling
- Awareness of starvation and aging as a mitigation
Common Mistakes
- Confusing CPU scheduling with I/O or disk scheduling
- Thinking Shortest-Job-First is always practical (it needs future burst-time knowledge)
- Forgetting that priority scheduling can starve low-priority tasks
- Not distinguishing preemptive from non-preemptive scheduling
Best Answer (HR Friendly)
“CPU scheduling is how the operating system decides, at any moment, which waiting task actually gets to use the processor next. Different strategies balance fairness, speed, and responsiveness differently — for example, giving everyone equal turns versus favoring the most urgent tasks — and a good scheduler makes sure nothing gets stuck waiting forever.”
Code Example
#define QUANTUM 10 /* time units per turn */
void round_robin(struct process *ready_queue[], int n) {
int remaining[n];
for (int i = 0; i < n; i++) remaining[i] = ready_queue[i]->burst_time;
int done = 0;
while (done < n) {
for (int i = 0; i < n; i++) {
if (remaining[i] <= 0) continue;
int slice = (remaining[i] < QUANTUM) ? remaining[i] : QUANTUM;
run_process(ready_queue[i], slice); /* run for one quantum */
remaining[i] -= slice;
if (remaining[i] <= 0) done++; /* process finished */
}
}
}Follow-up Questions
- What is the difference between preemptive and non-preemptive scheduling?
- How does Shortest-Job-First minimize average waiting time?
- What is starvation and how does aging fix it?
- How does multi-core scheduling differ from single-core scheduling?
MCQ Practice
1. Which scheduling algorithm gives every process a fixed time slice in rotation?
Round Robin allocates each process a fixed time quantum before moving to the next, cycling through the ready queue.
2. Priority scheduling risks which problem for low-priority tasks?
Without aging, low-priority tasks can be perpetually skipped in favor of higher-priority arrivals — this is starvation.
3. What distinguishes preemptive from non-preemptive scheduling?
Preemptive scheduling can forcibly suspend a running task to let another run; non-preemptive scheduling waits for the task to finish or block.
Flash Cards
What is CPU scheduling? — The OS mechanism deciding which ready task runs on the CPU next.
Name two scheduling algorithms. — First-Come-First-Served and Round Robin (also Shortest-Job-First, Priority Scheduling).
Preemptive vs non-preemptive? — Preemptive can interrupt a running task; non-preemptive lets it run to completion or a block.
What is aging in scheduling? — Gradually raising a waiting task’s priority over time to prevent starvation.