PreviousNext

Thread Scheduling

Threads are scheduled according to their scheduling priority and how the scheduling policy treats those priorities. To understand the discussion in this topic, you must understand the concepts in the following topics:

· Scheduling Policy Attribute discusses scheduling policies, including the way in which each policy handles thread scheduling priority.

· Scheduling Priority Attribute discusses thread scheduling priorities.

· Inherit Scheduling Attribute discusses inheritance of scheduling attributes by created threads.

To specify the minimum or maximum priority, use the appropriate symbol; for example, PRI_OTHER_MIN or PRI_OTHER_MAX. To specify a value between the minimum and maximum priority, use an appropriate arithmetic expression.

For example, to specify a priority midway between the minimum and maximum for the default scheduling policy, specify the following concept using your programming language's syntax:

pri_other_mid = (PRI_OTHER_MIN + PRI_OTHER_MAX)/2

If your expression results in a value outside the range of minimum to maximum, an error results when you use it. Priority values are integers.

To show results of the different scheduling policies, consider the following example: a program has four threads, called threads A, B, C, and D. For each scheduling policy, three scheduling priorities have been defined: minimum, middle, and maximum. The threads have the priorities shown in the following table.


Sample Thread Priorities

Thread Priority
A Minimum
B Middle
C Middle
D Maximum
The three figures below show execution flows, depending on whether the threads use the SCHED_FIFO, SCHED_RR, or SCHED_OTHER (default) scheduling policy. Assume that all waiting threads are ready to execute when the current thread waits or terminates and that no higher-priority thread is awakened while a thread is executing (during the flow shown in each figure).

The following figure shows a flow with SCHED_FIFO (First In, First Out) scheduling.


Flow with SCHED_FIFO Scheduling

Thread D executes until it waits or terminates, then Thread B starts because it has been waiting longer than Thread C and it executes until it waits or terminates, then Thread C executes until it waits or terminates, then Thread A executes.

The following figure shows a flow with SCHED_RR (Round Robin) scheduling.


Flow with SCHED_RR Scheduling

All four threads are timesliced. Threads with higher priority are generally scheduled when more than one thread is ready to run; however, to ensure fairness, all threads are given some time. The effective priority of threads may be modified over time by the scheduler, depending on the use of processor resources.

Thread D executes until it waits or terminates, then threads B and C are timesliced because they both have middle priority, then thread A executes.

The following figure shows a flow with SCHED_OTHER (default) scheduling.


Flow with SCHED_OTHER Scheduling

Thread D executes until it waits or terminates; then threads B, C, and A are timesliced, even though thread A has a lower priority than the other two. Thread A receives less execution time than thread B or C if either is ready to execute as often as thread A is. However, the default scheduling policy protects thread A against being blocked from executing indefinitely.

Because low-priority threads eventually run, the default scheduling policy protects against the problem of priority inversion discussed in Programming with Threads.