๐ง What is a CPU Scheduler?
The CPU Scheduler is a component of the OS that selects one process from the ready queue and allocates the CPU to it.
It decides which process runs next when:
-
A process finishes or is preempted
-
A process moves from waiting to ready
-
A new process is created
๐ When Does CPU Scheduling Happen?
Here are the scheduling decision points:
-
โ A process terminates
-
โณ A process switches from running to waiting (e.g., I/O)
-
๐ A process switches from running to ready (e.g., preemption)
-
๐ฅ A process switches from waiting to ready (e.g., I/O done)
Points 3 and 4 allow preemptive scheduling (Round Robin, Priority Preemptive)
Points 1 and 2 are enough for non-preemptive scheduling (FCFS, SJF Non-Preemptive)
๐ฆ How the CPU Scheduler Works
[ Ready Queue ]
โ
[ CPU Scheduler ] โโ chooses โ [ Running Process ]
It uses a scheduling algorithm (e.g., Round Robin, SJF, Priority)
to decide which process gets the CPU next.
๐ฏ Responsibilities of the CPU Scheduler
| Task | Description |
|---|---|
| ๐งฎ Choose process | Pick from ready queue |
| ๐ง Apply scheduling policy | FCFS, SJF, RR, Priority, etc. |
| ๐ Initiate context switch | Save old process state, load new one |
| ๐ฏ Optimize goals | Maximize CPU use, minimize waiting, ensure fairness |
๐ฅ Scheduling Criteria
A good CPU scheduler tries to optimize:
| Metric | Goal |
|---|---|
| โณ Waiting Time | Keep as low as possible |
| ๐ Turnaround Time | Time from submission to completion |
| ๐ Response Time | For interactive tasks |
| โก CPU Utilization | Keep CPU busy |
| ๐ Throughput | Max number of processes finished per unit time |
| โ๏ธ Fairness | No starvation or over-prioritization |
๐งฐ Example: Round Robin Scheduler (Preemptive)
-
All processes in ready queue
-
CPU Scheduler picks one
-
Assigns it CPU for time quantum (say 100 ms)
-
After quantum, switches to next ready process
โ
Fair
โ
Responsive
โ ๏ธ High context switch overhead if quantum is too short
๐ CPU Scheduler vs Long-Term & Medium-Term Scheduler
| Scheduler | Purpose |
|---|---|
| Long-Term | Controls admission of jobs into system |
| Medium-Term | Swaps processes in/out of memory |
| โ Short-Term / CPU Scheduler | Chooses which process runs next on CPU |
๐ง Interview-Ready Definition:
The CPU Scheduler is the OS component responsible for selecting a process from the ready queue and assigning it to the CPU based on a scheduling algorithm. Itโs critical for ensuring fairness, responsiveness, and efficient CPU utilization in multitasking systems.
โ๏ธ Real-World Analogy
Think of the CPU as a doctor, and the ready queue as a waiting room full of patients.
The CPU Scheduler is the receptionist who decides which patient sees the doctor next, based on urgency, arrival time, or other rules.