What is CPU Scheduling?

CPU Scheduling is the process of deciding which process gets the CPU next.

Since a CPU (single core) can execute only one process/thread at a time, the Operating System uses a CPU Scheduler to decide the execution order.

Example: Processes waiting for the CPU:

Chrome
Spotify
VS Code
Discord

The scheduler decides which one should execute first.

Why is CPU Scheduling Needed?

Imagine three processes:

Process CPU Time
Chrome 10 sec
Spotify 2 sec
VS Code 1 sec

If Chrome executes first, Spotify and VS Code must wait 10 seconds.

A better strategy may be to execute shorter jobs first so that more processes finish quickly.

This is why different scheduling algorithms exist.

CPU Scheduling Algorithms

1. First Come First Serve (FCFS)

Processes are executed in the order they arrive.

Example

Arrival Order

Chrome (10s)
Spotify (2s)
VS Code (1s)

Execution:

Chrome → Spotify → VS Code

Pros

Cons


2. Shortest Job First (SJF)

The process with the shortest CPU burst executes first.

Example

Chrome (10s)
Spotify (2s)
VS Code (1s)

Execution:

VS Code → Spotify → Chrome

Pros

Cons

Example:

Chrome (10s)  ← Waiting

Calculator (1s)
Notepad (1s)
Terminal (1s)

Chrome may keep waiting indefinitely.

3. Round Robin (RR)

Each process gets a fixed amount of CPU time called the Time Quantum.

If it doesn't finish within its quantum, it goes back to the end of the queue.

Example

Time Quantum = 2 seconds

Chrome (10s)
Spotify (2s)
VS Code (1s)

Execution:

Chrome → 2s
Spotify → Finished
VS Code → Finished
Chrome → 2s
Chrome → ...

Pros

Cons

Time Quantum too small


4. Priority Scheduling

Each process is assigned a priority.

The scheduler executes the highest-priority process first.

Example (Netflix as an operating system)

Watch Movie      → High Priority
Download Movie   → Medium Priority
App Update       → Low Priority

Movie playback should receive CPU first because it is latency-sensitive, while downloads and updates can wait.

Pros

Cons


Interview Points

Why not always use SJF?

Because long-running processes may starve.

Why not always use Round Robin?

Choosing the wrong Time Quantum can either:

Which algorithm do real operating systems use?

Modern operating systems (Linux, Windows, macOS) do not use a single algorithm. They use hybrid schedulers that combine ideas from: