13.2 Scheduling Policies
The operating system supports four scheduling policies for threads. A thread object's scheduling policy is determined by the setting of the scheduling policy attribute in its thread attributes object at the time the thread is created. The scheduling policy attribute describes how the thread is scheduled for execution relative to the other threads in the program.
A thread has one of the following scheduling policies described in the following subsections. Only the
SCHED_FIFO
and
SCHED_RR
scheduling policies are portable. The symbol name
SCHED_OTHER
itself is portable, but it specifies behavior that may vary from one POSIX.1c standard implementation to another.
All threads, regardless of scheduling policy, are scheduled in a single priority space from 0 to 63. That is, a thread at priority 63 will always be chosen to run before a thread at priority 62 even if the lower priority thread is
SCHED_FIFO
while the higher priority thread is
SCHED_OTHER
. Unblocking a thread in
SCHED_FIFO
or
SCHED_RR
policy, with higher priority, will preempt a running thread of lower priority. Unblocking a thread in
SCHED_OTHER
(or
SCHED_FG_NP
or
SCHED_BG_NP
) will not preempt a running thread. Threads with
SCHED_FIFO
or
SCHED_RR
policy will be awakened in priority order from the mutex, condition variable, and read-write lock waits. Threads in other policies will be awakened in priority order if
SCHED_RR
or
SCHED_FIFO
threads are concurrently waiting on the same object; otherwise they are awakened in FIFO order.
For more information about the scheduling policies supported by the operating system, see Guide to the POSIX Threads Library.
13.2.1 SCHED_FIFO
(first-in/first-out scheduling)
SCHED_FIFO
Under this scheduling policy, the highest priority thread runs until it blocks. If there is more than one thread with the same priority and that priority is the highest among other threads, the first thread to begin running continues until it blocks. If a thread with this policy becomes ready, and it has a higher priority than the currently running thread, then it preempts the current thread and begins running immediately.
13.2.2 SCHED_RR
(round-robin scheduling)
SCHED_RR
Under this scheduling policy, the highest priority thread runs until it blocks; however, threads of equal priority, if that priority is the highest among other threads, are timesliced. Timeslicing is a mechanism that ensures that every thread is allowed time to execute by preempting running threads at fixed intervals.) If a thread with this policy becomes ready, and it has a higher priority than the currently running thread, then it preempts the current thread and begins running immediately.
13.2.3 SCHED_FG_NP
(foreground scheduling; also known as SCHED_OTHER
)
SCHED_FG_NP
SCHED_OTHER
This is default scheduling policy. Under this scheduling policy, all threads are timesliced. All threads receive some scheduling regardless of priority. Therefore, no thread is completely denied execution time. Higher priority threads receive more execution time than lower priority threads. Threads with this scheduling policy can be denied execution time by
SCHED_FIFO
or
SCHED_RR
threads.
13.2.4 SCHED_BG_NP
(background scheduling)
SCHED_BG_NP
Under this scheduling policy, like the default
SCHED_FG_NP
scheduling policy, all threads receive some scheduling, regardless of priority. However, background threads can be denied execution time by
SCHED_FIFO
or
SCHED_RR
threads, and receive less execution time than
SCHED_FG_NP
threads.