CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 3 of 7 · 3 min read

Policies

Think of it this way. A hospital has a normal ward, a night-shift ward and an emergency room, each with its own rules for who is seen first. Linux likewise has several scheduling policies, and a task belongs to exactly one. The analogy stops in one place: a patient is moved between wards by staff, while a task can ask for its own policy, when its owner allows it.

In plain words: Linux groups the policies into classes, checked from the top. A task of a higher class always goes before a task of a lower one.

Class Policies Meaning
Deadline SCHED_DEADLINE Earliest deadline first, with a stated run time, deadline and period (page 6)
Real-time SCHED_FIFO, SCHED_RR Fixed priorities 1 to 99, always ahead of normal tasks
Normal (fair) SCHED_OTHER, SCHED_BATCH, SCHED_IDLE Shares by weight (page 5)
  • SCHED_OTHER (also shown as SCHED_NORMAL) is the default. Weights come from the nice value.
  • SCHED_BATCH is for non-interactive work: the same shares, but the scheduler assumes it does not need quick wake-ups.
  • SCHED_IDLE is for work that should run only when nothing else wants the processor: its weight is far lower than nice 19.
  • SCHED_FIFO runs a task until it waits, ends or is preempted by a higher real-time priority. There is no time slice.
  • SCHED_RR is the same, with round robin among tasks of equal priority and a time slice.

The shell commands change them. chrt -p <pid> shows a task's policy, chrt -r 10 cmd runs a command under SCHED_RR at priority 10, chrt -i 0 cmd under SCHED_IDLE, nice -n 10 cmd sets the nice value, and taskset -c 0 cmd pins a command to processor 0. A program can use the system calls:

struct sched_param sp = { .sched_priority = 10 };
if (sched_setscheduler(0, SCHED_RR, &sp) == -1) {   /* 0 means this process */
    printf("sched_setscheduler failed: %s\n", strerror(errno));
    return 1;
}
sched_rr_get_interval(0, &ts);                      /* time slice of this task */

Running it as the superuser, then as an ordinary user:

before: policy 0 (0 = SCHED_OTHER)
after: policy 2 (2 = SCHED_RR), priority 10, time slice 100 ms

before: policy 0 (0 = SCHED_OTHER)
sched_setscheduler failed: Operation not permitted

An ordinary user cannot choose a real-time policy (the error is EPERM), because a real-time task could take the whole machine. The measured time slice of SCHED_RR is 100 ms, much longer than the few milliseconds of normal tasks measured on page 3 (the value comes from /proc/sys/kernel/sched_rr_timeslice_ms, which printed 100).

Scheduling policy

A scheduling policy on Linux is the rule that decides how the kernel treats a task: the real-time policies SCHED_FIFO and SCHED_RR use fixed priorities from 1 to 99, SCHED_DEADLINE uses deadlines, and the normal policies SCHED_OTHER, SCHED_BATCH and SCHED_IDLE share the processor by weight.

Example: chrt -r 10 ./prog starts prog under SCHED_RR at priority 10, ahead of every normal task.

Check yourself: 1. Which class always runs before the normal class? 2. Why does sched_setscheduler fail for an ordinary user? 3. Which policy would suit a nightly backup job, and why?