June 24, 2026
How the Linux Scheduler Actually Works: How Your CPU Decides What Runs Next (P1)
A beginner-friendly but deep dive into the hidden Linux system that makes multitasking possible
By Alwinaji
4 min read
A beginner-friendly but deep dive into the hidden Linux system that makes multitasking possible
You open:
firefoxfirefoxSpotify is playing.
VS Code is open.
Terminal running:
docker compose updocker compose upChrome has:
17 tabs17 tabsYouTube playing video.
Background updates running.
Question:
How does your computer not explode?
You only have:
One CPU_ (or a few CPU cores)_
But Linux appears to run:
Hundreds of programsHundreds of programsat the same time.
Feels magical.
Question:
How does Linux decide:
What runs next?
Why does:
vimvimfeel responsive,
while:
yes > /dev/nullyes > /dev/nulldoesn't freeze your machine?
How does Linux prevent:
One greedy processOne greedy processfrom stealing everything?
The answer is:
The Linux Scheduler
Today we'll deeply understand:
Why schedulers exist What problem schedulers solve Process lifecycle explained simply Context switching Time slicing Preemptive multitasking CPU scheduling goals Hands-on Linux labs Observe scheduling live
And yes —
We'll keep it beginner-friendly.
Let's begin.
First: The Problem
Imagine:
You own:
1 chef1 chefBut restaurant has:
100 customers100 customersQuestion:
Who gets served first?
What if one customer says:
"I need ALL attention forever.""I need ALL attention forever."Restaurant breaks.
Chaos.
This is exactly:
CPU scheduling problem.
Your CPU is:
ChefChefPrograms are:
CustomersCustomersLinux scheduler is:
Restaurant managerRestaurant managerdeciding:
Who gets CPU time
When
For how longWho gets CPU time
When
For how longHuge realization:
Your CPU can only execute one instruction per core at a time.
Everything else is:
Illusion.
What Is a Scheduler?
Simple definition:
A scheduler decides which process gets CPU time next.
Very important wording:
CPU timeCPU timeBecause CPU is limited.
Question:
How can Linux run:
Firefox
Spotify
VS Code
Terminal
Kernel tasksFirefox
Spotify
VS Code
Terminal
Kernel taskssimultaneously?
Answer:
Linux rapidly switches between them.
Extremely fast.
Feels parallel.
Actually:
Controlled switching.
The Big Mental Model
Without scheduler:
Process A starts
↓
Never stops
↓
Everything else frozenProcess A starts
↓
Never stops
↓
Everything else frozenTerrible.
With scheduler:
Process A
↓
Process B
↓
Process C
↓
Process A
↓
Process DProcess A
↓
Process B
↓
Process C
↓
Process A
↓
Process DHappens:
Thousands of times per second.
Feels like:
Everything running togetherEverything running togetherBut really:
Tiny CPU turns.
CPU Time Is Shared
Question:
What happens when:
yes > /dev/nullyes > /dev/nullruns?
This command:
Never stops
Consumes CPU foreverNever stops
Consumes CPU foreverWithout scheduler:
Entire system freezesEntire system freezesBad.
Linux instead says:
You get small CPU slice.
Then wait.You get small CPU slice.
Then wait.Very important concept.
Process States Explained
Linux processes move between:
Different states.
Think:
Queue at airport.
1. Running
Currently using CPU
Example:
Firefox rendering webpageFirefox rendering webpageState:
RUNNINGRUNNINGOnly:
One process per CPU coreOne process per CPU corecan be here.
Important realization.
2. Ready (Runnable)
Waiting for CPU
Example:
VS Code wants CPUVS Code wants CPUbut CPU busy.
Linux says:
Wait your turn.Wait your turn.State:
READYREADYVery common.
3. Sleeping
Waiting for something
Example:
Browser waiting for:
Network responseNetwork responseOr:
sleep 10sleep 10Process intentionally idle.
State:
SLEEPINGSLEEPINGNot consuming CPU.
Huge misconception beginners have:
Sleeping ≠ dead.
4. Zombie
Finished but not cleaned up
Strange Linux concept.
Process exited.
But parent didn't:
Collect exit statusCollect exit statusResult:
Zombie processZombie processWe'll cover deeply later.
Visual Process Lifecycle
waiting for CPU
↑
|
RUNNING → READY → RUNNING
↓
SLEEPING
↓
READYwaiting for CPU
↑
|
RUNNING → READY → RUNNING
↓
SLEEPING
↓
READYLinux constantly moves processes.
Very dynamic system.
Hands-On Lab 1 — Observe Processes
Run:
toptopObserve:
PID
CPU %
MEM %
STATEPID
CPU %
MEM %
STATEInteresting.
Press:
Shift + PShift + PSort by CPU.
Question:
Who is consuming CPU right now?
You're watching:
Scheduler decisions live.
Context Switching Explained
Question:
How does Linux jump between programs?
Example:
CPU currently running:
FirefoxFirefoxSuddenly:
User moves mouse.
Linux decides:
Terminal should runTerminal should runQuestion:
How does CPU remember:
Firefox progress?Firefox progress?Answer:
Context switching.
What Is Context?
Context means:
CPU stateCPU stateThings like:
Registers
Program counter
Stack pointer
Memory stateRegisters
Program counter
Stack pointer
Memory stateLinux saves:
Process A stateProcess A stateThen loads:
Process B stateProcess B stateCPU continues.
Feels magical.
Actually:
State saving.
Visual Context Switch
Firefox running
↓ interrupt
Save state
↓
Load terminal state
↓
Terminal runningFirefox running
↓ interrupt
Save state
↓
Load terminal state
↓
Terminal runningThen later:
Save terminal
↓
Restore FirefoxSave terminal
↓
Restore FirefoxHuge Linux concept.
Why Context Switching Is Expensive
Question:
Why not switch constantly?
Because:
Switching costs work.
Linux must:
Save registers
Save execution state
Load new process state
Invalidate cachesSave registers
Save execution state
Load new process state
Invalidate cachesToo many switches:
Performance dropsPerformance dropsScheduler balances:
Responsiveness
vs
EfficiencyResponsiveness
vs
EfficiencyBeautiful engineering.
Time Slicing
How Linux shares CPU fairly
Question:
How long should process run?
Forever?
No.
Linux gives:
Small CPU window.
Called:
Time slice (quantum)
Example:
10 milliseconds10 millisecondsThen:
Next processNext processCPU rotates.
Visual Time Slicing
CPU Timeline
Firefox → VSCode → Terminal
5ms 5ms 5msCPU Timeline
Firefox → VSCode → Terminal
5ms 5ms 5msFeels simultaneous.
Reality:
Tiny slices.
Mind blown?
Preemptive Multitasking
Very important concept.
Old systems:
Process voluntarily gives CPUProcess voluntarily gives CPUProblem:
Bad process says:
No.No.System freezes.
Linux instead:
Forces process to stop
Example:
Time slice over.Time slice over.Linux interrupts:
Enough.
Your turn ended.Enough.
Your turn ended.This is called:
Preemptive multitasking
Huge reason Linux feels responsive.
Hands-On Lab 2 — Create CPU Hog
Run:
yes > /dev/nullyes > /dev/nullCPU usage spikes.
Open another terminal:
toptopInteresting:
Machine still usable.
Question:
Why?
Scheduler.
Linux says:
You get CPU,
but not forever.You get CPU,
but not forever.Press:
CTRL + CCTRL + Cto stop.
Observe Scheduling Live
Run:
htophtopInstall if missing:
sudo apt install htopsudo apt install htopObserve:
CPU bars moving.
Processes jumping.
Why?
Scheduler constantly reshuffling.
Very cool.
What Makes Good Scheduling?
Linux scheduler tries to optimize:
1. Fairness
No process should starve.
Example:
Bad:
Chrome gets everything
Terminal frozenChrome gets everything
Terminal frozenGood:
Everyone gets turnsEveryone gets turns2. Responsiveness
Typing should feel instant.
Example:
You press:
AAScreen updates immediately.
Linux prioritizes:
Interactive tasksInteractive tasks3. Throughput
Complete lots of work.
Example:
Servers handling:
Thousands of requestsThousands of requestsNeed efficiency.
4. Low Latency
Fast reaction time.
Important for:
Games
Audio
Real-time systemsGames
Audio
Real-time systemsHands-On Lab 3 — Observe Scheduling Fairness
Open terminal 1:
Run:
yes > /dev/nullyes > /dev/nullOpen terminal 2:
Run again:
yes > /dev/nullyes > /dev/nullNow:
toptopObserve:
CPU split.
Example:
50%
50%50%
50%Huge realization:
Scheduler sharing CPU.
Nice Values (Preview)
Question:
Can Linux prefer some programs?
Yes.
Example:
nice -n 10 yes > /dev/nullnice -n 10 yes > /dev/nullMeans:
Lower priorityLower priorityWe'll deeply cover this in:
Part 2 (CFS Scheduler)
Multi-Core CPUs
Question:
What if:
4 cores4 coresexist?
Linux can run:
4 processes simultaneously4 processes simultaneouslyOne per core.
Scheduler balances work across CPUs.
Very clever.
Common Beginner Misconceptions
1. "Everything runs at same time"
Not exactly.
Reality:
Rapid switchingRapid switching2. "One app uses entire CPU"
Scheduler prevents abuse.
3. "Sleeping process uses CPU"
No.
Sleeping means:
WaitingWaiting4. "CPU = infinite"
No.
Scheduler carefully shares limited resource.
Mini Challenge
Question 1:
Why doesn't:
yes > /dev/nullyes > /dev/nullfreeze Linux?
Question 2:
What is:
Context switching?
Question 3:
What is:
Time slicingTime slicing?
Question 4:
Why is preemption important?
Think deeply.
The Big Mental Model
Linux scheduler:
Many processes
↓
CPU competition
↓
Scheduler decisions
↓
Tiny CPU slices
↓
Feels simultaneousMany processes
↓
CPU competition
↓
Scheduler decisions
↓
Tiny CPU slices
↓
Feels simultaneousBeautiful engineering.
Final Thoughts
At first:
Multitasking feels magical.
Browser.
Music.
Terminal.
Docker.
Everything running together.
Feels impossible.
But underneath?
Linux is doing something incredibly smart.
Rapidly deciding:
Who runs next
How long
When to stopWho runs next
How long
When to stopthousands of times every second.
And that hidden system is called:
The Linux Scheduler
And once this clicks…
You stop seeing multitasking as magic.
You finally understand:
How Linux decides what runs next.