Scheduling, priorities and nice

Intermediate · 11 min

How Linux shares CPUs between runnable threads, what a nice value actually buys, real-time policies with chrt, and pinning work to CPUs with taskset.

Before thisProcesses through /proc

Learn Linux TV · 14 min · from 2:38

Scheduling, priorities and nice

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Ordinary threads run under SCHED_OTHER, which since Linux 6.6 is implemented by EEVDF instead of CFS. Both share CPU in proportion to weights set by the nice value.
  • Nice runs from -20 to 19. Each step is a factor of about 1.25 in weight, so two busy processes at nice 0 and nice 10 split a CPU about 90/10, not 'a bit less'.
  • Nice only matters when CPUs are contended. On an idle machine a nice 19 job runs at full speed.
  • Unprivileged users can only raise their nice value. Lowering it needs root or a raised RLIMIT_NICE.
  • With autogrouping or cgroups, nice only compares threads within the same group, so renicing a process may change less than you expect.
  • SCHED_FIFO and SCHED_RR are real-time policies set with chrt. They always beat normal threads, so the kernel by default keeps 5% of CPU time for everyone else.
  • taskset sets CPU affinity, the set of CPUs a thread may run on. It is for isolation and cache locality, not for speed in general.

More work than CPUs

A server with eight CPUs and twenty threads that want to run has to choose, many times a second, which eight run now. That choice is the scheduler’s job. Most of the time its defaults are right and you never think about it. You think about it when a nightly batch job slows the web app, when a latency-sensitive process stutters, or when someone asks why nice did nothing.

The computer science lesson on CPU scheduling covers the algorithms in general. This one is about the knobs Linux gives you.

Policies

sched(7) describes the policies a thread can have:

  • SCHED_OTHER: the default for nearly everything. CPU is shared fairly, weighted by nice value.
  • SCHED_BATCH: like SCHED_OTHER, but the scheduler assumes the thread is CPU-bound and penalises it slightly for wake-ups. Good for batch jobs.
  • SCHED_IDLE: runs only when nothing else wants the CPU, weaker even than nice 19.
  • SCHED_FIFO and SCHED_RR: real-time, with priorities 1 to 99. A runnable real-time thread always beats every normal one.
  • SCHED_DEADLINE: real-time by runtime, deadline and period, for specialist work.

CFS, then EEVDF

For sixteen years, from Linux 2.6.23 in 2007, SCHED_OTHER was implemented by CFS, the Completely Fair Scheduler, which tracked how much “virtual runtime” each thread had used and ran the one with the least. The kernel’s own documentation, checked against the 7.4-rc4 tree, says the kernel began moving to EEVDF, Earliest Eligible Virtual Deadline First, in version 6.6. EEVDF still tracks virtual runtime, but it computes a “lag” per task, whether it is owed CPU or has had too much, and among tasks that are owed time, picks the one with the earliest virtual deadline. Tasks asking for short slices get earlier deadlines, which helps latency-sensitive work.

sched(7) in man-pages 6.19 still opens with “the default scheduler is CFS”. That sentence is out of date. For your purposes the knobs below did not change: nice values and weights work the same way under both.

What a nice value buys

Nice runs from -20, most favoured, to 19, least. The scheduler turns it into a weight, and threads competing for one CPU share it in proportion to their weights. sched(7) says each step is a factor of 1.25. To see what that means, I took the weight table from the kernel source, sched_prio_to_weight in kernel/sched/core.c (nice 0 is 1024, nice 19 is 15, nice -20 is 88761), and computed the split for two CPU-bound threads on one CPU. This ran on this Mac in Python, a calculation from the kernel’s table, not a measurement on Linux:

nice   0 vs nice   1:  55.5% /  44.5%
nice   0 vs nice   5:  75.3% /  24.7%
nice   0 vs nice  10:  90.3% /   9.7%
nice   0 vs nice  19:  98.6% /   1.4%
nice  -5 vs nice   0:  75.3% /  24.7%

Two lessons from that table. Nice is relative: -5 against 0 is the same split as 0 against 5. And nice 10 is not “a bit slower”, it is one tenth of the CPU when there is a fight. When there is no fight, it is all of the CPU. Nice changes shares, not speed.

Changing it

These commands are from the util-linux and coreutils man pages. I did not run them here:

nice -n 10 ./nightly-report         # start at nice 10
renice -n 5 -p 4123                 # change a running process
ps -o pid,ni,pri,cls,comm -p 4123   # see nice and policy (CLS: TS=other, FF=fifo, RR=rr)

renice(1) states the rule: users other than root can only change their own processes, and can only raise the nice value, which they cannot undo unless RLIMIT_NICE allows it. Lowering below 0 is root’s job. For services, set Nice= in the unit, as the logrotate service in the timers lesson did with Nice=19.

There is a catch sched(7) spells out. With group scheduling, a thread’s nice value only competes against threads in the same task group. A CPU cgroup is a task group, and with autogrouping enabled, which the man page says is the default in various distributions, each terminal session is one too. So renicing a build in one terminal does not make it yield to a job in another session. On a systemd server, services sit in their own cgroups, and the fair way to divide CPU between them is CPUWeight=, coming in the cgroups lesson.

Real-time with chrt

chrt sets policy and real-time priority:

chrt -f -p 50 4123     # SCHED_FIFO, priority 50, on a running process
chrt -p 4123           # show its policy and priority
chrt -m                # list valid priority ranges
chrt -i 0 ./scrub      # SCHED_IDLE

A SCHED_FIFO thread runs until it blocks, yields or is preempted by a higher real-time priority. A bug that spins becomes a machine that does not respond. The kernel guards against total lock-up with /proc/sys/kernel/sched_rt_runtime_us, default 950,000 out of a 1,000,000 microsecond period, which per sched(7) reserves 5% of CPU for everything that is not real-time. Real-time policies are for audio, industrial control and similar latency-bound work, not for making a web server faster.

Pinning with taskset

Affinity is the set of CPUs a thread may run on. /proc/<pid>/status shows it as Cpus_allowed_list. taskset reads and changes it:

taskset -c 2,3 ./worker     # start limited to CPUs 2 and 3
taskset -p 4123             # show the mask of a running process
taskset -pc 0-1 4123        # change it; -p and -c must be grouped

Pinning helps when you want to keep noisy work off the CPUs a latency-sensitive process uses, or keep a process near its cache or memory node. In general it makes things slower, because the scheduler can no longer move work to an idle CPU. The systemd equivalent is CPUAffinity= in a unit.

About the video

The embed is Learn Linux TV’s “Linux Process Tuning Made Easy: Understanding nice and renice”, December 2023, from 2:38 to the end, which skips the intro and the sponsor segment. Its chapters cover a ps refresher, reading niceness in ps output, changing a running process’s priority, more on renice, starting a process with nice, and a difference between the two commands. It covers nice well and nothing else in this lesson: no EEVDF, weights, group scheduling, real-time policies or affinity. Those parts are written from sched(7), the chrt, taskset and renice man pages and the kernel’s EEVDF documentation. English was judged from the channel and English auto-captions, the content from chapters.

What was run: the weight-share calculation above, in Python on macOS, from the table in the Linux 7.4-rc4 source. No scheduling command was run, because there is no Linux machine here. Docs checked 2026-11-28.

Where this fits

CPU is one resource a process can hog. Open files and process counts are others, and the kernel caps those per process: resource limits with ulimit.

Check yourself

  1. 01A backup job at nice 19 still takes all of one CPU on an otherwise idle server. Is nice broken?

    No. Nice sets a share of CPU under contention. With nothing else wanting that CPU, the job gets all of it.

  2. 02As an ordinary user you renice your job from 10 to 0 and get 'Permission denied'. Why?

    Unprivileged users may only increase the nice value. Going back down needs root, CAP_SYS_NICE, or an RLIMIT_NICE limit that allows it.

  3. 03Why is running a busy loop under SCHED_FIFO priority 50 dangerous?

    A SCHED_FIFO thread runs until it blocks or yields and preempts every normal thread. Only the kernel's real-time throttling, 5% of CPU by default, leaves the rest of the system any time.

  4. 04How do you pin an already running process 4123 to CPUs 2 and 3?

    `taskset -pc 2,3 4123`. The -p and -c options must be given together for a running process.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark