Language and Page Formatting Options
4.2. CPU Scheduling
The scheduler is responsible for keeping the CPUs in the system busy. The Linux scheduler implements a number of scheduling policies, which determine when and for how long a thread runs on a particular CPU core.
Scheduling policies are divided into two major categories:
- Realtime policies
- Normal policies
4.2.1. Realtime scheduling policies
Realtime threads are scheduled first, and normal threads are scheduled after all realtime threads have been scheduled.
The realtime policies are used for time-critical tasks that must complete without interruptions.
- This policy is also referred to as static priority scheduling, because it defines a fixed priority (between 1 and 99) for each thread. The scheduler scans a list of SCHED_FIFO threads in priority order and schedules the highest priority thread that is ready to run. This thread runs until it blocks, exits, or is preempted by a higher priority thread that is ready to run.Even the lowest priority realtime thread will be scheduled ahead of any thread with a non-realtime policy; if only one realtime thread exists, the
SCHED_FIFOpriority value does not matter.
- A round-robin variant of the
SCHED_RRthreads are also given a fixed priority between 1 and 99. However, threads with the same priority are scheduled round-robin style within a certain quantum, or time slice. The
sched_rr_get_interval(2)system call returns the value of the time slice, but the duration of the time slice cannot be set by a user. This policy is useful if you need multiple thread to run at the same priority.
For more detailed information about the defined semantics of the realtime scheduling policies, refer to the IEEE 1003.1 POSIX standard under System Interfaces — Realtime, which is available from http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_08.html.
Best practice in defining thread priority is to start low and increase priority only when a legitimate latency is identified. Realtime threads are not time-sliced like normal threads;
SCHED_FIFOthreads run until they block, exit, or are pre-empted by a thread with a higher priority. Setting a priority of 99 is therefore not recommended, as this places your process at the same priority level as migration and watchdog threads. If these threads are blocked because your thread goes into a computational loop, they will not be able to run. Uniprocessor systems will eventually lock up in this situation.
In the Linux kernel, the
SCHED_FIFOpolicy includes a bandwidth cap mechanism. This protects realtime application programmers from realtime tasks that might monopolize the CPU. This mechanism can be adjusted through the following
/procfile system parameters:
- Defines the time period to be considered one hundred percent of CPU bandwidth, in microseconds ('us' being the closest equivalent to 'µs' in plain text). The default value is 1000000µs, or 1 second.
- Defines the time period to be devoted to running realtime threads, in microseconds ('us' being the closest equivalent to 'µs' in plain text). The default value is 950000µs, or 0.95 seconds.