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

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.

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.

Count the CPUs the scheduler can use
nproc

The 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.

Show the scheduling policy of your shell
chrt -p $

When fair sharing goes wrong

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.

  1. Count the logical CPUs the scheduler can place threads on.

    nproc

    What you should see: The output is one number: how many logical CPUs the scheduler can place threads on.

  2. 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.