The scheduler: who gets a CPU, and for how long?
Video 071 of 140 in Virtualization Foundations, in the unit “Processes, threads and scheduling”. 3:55, with English and Hindi audio.
A server has a few CPUs, but many threads want them. Who gets a CPU, and for how long? That question belongs to the scheduler, a part of the Linux kernel. This article explains how the scheduler takes turns between ready threads, why short time slices keep many programs responsive, and how to check the CPU count and scheduling policy from a shell. Virtual machines depend on the same rules, so the ideas here carry over to them.
What you will learn
- The scheduler decides which ready thread runs on a CPU, and for how long.
- A thread runs for a short time slice, then leaves when it waits, finishes or uses up its slice.
- Many ready threads on too few CPUs make every task feel slow.
- One program that never sleeps can take more than its fair share of a CPU.
- The nproc and chrt commands show the CPU count and the scheduling policy.
Why a scheduler exists
Early computers ran one job at a time. The costly processor often sat idle, waiting for slow tape drives and printers. In the early nineteen sixties, time-sharing systems let many users share one computer by switching between their jobs.
Today the same idea runs inside Linux. The scheduler switches between ready threads very often, on every CPU. A thread is a single flow of work inside a program, and many threads can wait for the same few CPUs at once.
How one turn on a CPU is decided
The scheduler keeps a waiting line of ready threads. Each one wants a turn on a CPU. Each turn follows four steps.
- Waiting line: the ready threads wait in line, each wanting a turn.
- Pick one: the scheduler chooses fairly, so over time each waiting thread gets a reasonable share of the CPU.
- Run a slice: the chosen thread runs for a short share of time, a few thousandths of a second.
- Switch out: the thread leaves the CPU when it waits, finishes, or uses up its slice.
When a slice is used up, the thread goes to the back of the line, and another thread takes the CPU. A thread that is waiting for data gives up the CPU early. A more urgent thread that wakes up can also take the CPU sooner.
Check the CPU count and the scheduling policy
Two commands show the scheduler's view from a shell. The first asks how many CPUs the scheduler can place threads on.
nprocThe second shows the scheduling policy of your shell, followed by its priority. A normal program uses SCHED_OTHER, the standard fair policy, and its name is written in capitals.
chrt -p $When fair sharing goes wrong
- One thread hogs the CPU: a program that never sleeps keeps taking its share, and the others wait.
- Too many ready threads: each one waits longer for its turn, so every task feels slow.
- The signs: slow screens, and a CPU that is almost never idle.
Note: When you see slow screens and a busy CPU, find out which program is using the time before you change anything else.
Count your CPUs and check the policy
You need a Linux terminal. You will count the CPUs the scheduler can use, then check the policy your shell runs under.
Count the logical CPUs the scheduler can place threads on.
nprocWhat you should see: The output is one number: how many logical CPUs the scheduler can place threads on.
Show the scheduling policy of your shell, then its priority.
chrt -p $What you should see: A normal program uses SCHED_OTHER, the standard fair policy.
Terms
- Scheduler
- The part of the kernel that decides which ready thread runs on a CPU, and for how long.
- Time slice
- A short share of time that a thread runs on a CPU before it may be switched out for another thread.
- Ready thread
- A thread that can run now and is waiting in line for a CPU.
Check yourself
What happens to a thread when its time slice is used up?
It leaves the CPU and goes to the back of the waiting line, and another ready thread takes the CPU.
Why can one program that never sleeps slow other programs down?
It keeps taking its share of the CPU, so the other ready threads wait longer for their turns.
Which command shows the scheduling policy of your shell?
chrt -p $ shows it. A normal program uses SCHED_OTHER, the standard fair policy.
Previous: Process states: running, waiting, stopped or zombie?. Next: The run queue and the load average.