Threads Module Platform Guide : Chapter 4 Linux Technical Information : Linux POSIX 1003.1c Thread Attribute Support
Linux POSIX 1003.1c Thread Attribute Support
This section describes the Linux POSIX 1003.1c thread attribute support.
Scheduling Attributes
This section describes the Linux POSIX 1003.1c implementation-specific support, behavior, and restrictions for thread scheduling attributes.
Start Policy
The start policy attribute is fully supported by the Linux implementation of the Threads Module and defaults to RW_THR_START_RUNNING.
Contention Scope
Support for contention scope is optional. The Threads Module determines whether contention scope is supported by testing for the definition of the macro _POSIX_THREAD_PRIORITY_SCHEDULING.
Linux does not support process-scope threads because each Linux thread is a separate Unix process. Linux has only one scheduler for all processes (see “GNU LinuxThreads”). Therefore, all threads have a fixed contention scope of RW_THR_SYSTEM_SCOPE.
You may query the contention scope and freely set it to RW_THR_SYSTEM_SCOPE (although it is already set to that by default); but any attempt to change the scope to RW_THR_PROCESS_SCOPE produces an exception.
Scheduling Inheritance Policy
The Linux POSIX implementation of the Threads Module fully supports the scheduling inheritance policy attribute, which defaults to RW_THR_INHERIT.
Concurrency Policy
Linux does not support process-scope threads because each Linux thread is a separate Unix process (see “GNU LinuxThreads”). Therefore, the concurrency policy attribute is not supported in the Linux implementation of the Threads Module. Attempts to get or set this attribute value results in exceptions.
Scheduling Policy
In POSIX 1003.1c-compliant systems, support for scheduling policy is optional. The Threads Module determines whether scheduling policy is supported by testing for the definition of the macro _POSIX_THREAD_PRIORITY_SCHEDULING.
The Linux POSIX implementation of the Threads Module supports all three scheduling policies as defined by the standard:
SCHED_FIFO — Specifies FIFO Scheduling, where threads run until preempted by a thread of higher priority, or until blocked. Thread priorities are set by the application; the system does not dynamically change a thread’s priority.
SCHED_RR — Selects round-robin scheduling, where the highest-priority threads runs until preempted by a thread of higher priority, until some time-quantum has elapsed, or until blocked. Threads possessing the same priority value get time-sliced and scheduled in a round-robin fashion. Thread priorities are set by the application; the system does not dynamically change a thread’s priority.
SCHED_OTHER — Selects the default scheduling policy for an implementation. This policy typically uses time-slicing with dynamic adjustments to priority and or time-slice quantum. According to the Linux manpage sched_setscheduler(2), under Linux, this policy is “based on the nice level … and increased for each time quantum the process [i.e. thread] is unable to run.”
Note that Linux POSIX limits the scheduling policies SCHED_RR and SCHED_FIFO to processes with superuser privileges. Therefore, the RWSchedulingPolicyvalues RW_THR_PREEMPTIVE and RW_THR_TIME_SLICED_FIXED are limited to superusers as well.
Table 7 shows how the Threads Module Linux POSIX implementation maps RWSchedulingPolicy values to the underlying POSIX 1003.1c policy values.
Table 7 – Linux: Mapping of RWSchedulingPolicy to POSIX 1003.1c values 
Threads Module RWSchedulingPolicy Values
POSIX 1003.1c Scheduling Policy
RW_THR_PREEMPTIVE
SCHED_FIFO
RW_THR_TIME_SLICED_FIXED
SCHED_RR
RW_THR_TIME_SLICED_DYNAMIC(RW_THR_OTHER may be used to set)
SCHED_OTHER
Attempts to set any other policy values result in an RWTHROperationNotAvailable exception. None of these policies may be explicitly requested unless the process has superuser privileges.
Note that the Threads Module has mapped two policy values to the same underlying policy, SCHED_OTHER. Calls to getSchedulingPolicy() return RW_THR_TIME_SLICED_DYNAMIC, since that value gives the most meaningful interpretation.
In order for a new thread’s scheduling policy to inherit by default from the creating thread, you must change the inheritance policy’s default value from RW_THR_EXPLICIT to RW_THR_INHERIT. If you do not set RW_THR_INHERIT, a new thread’s scheduling policy defaults to RW_THR_TIME_SLICED_DYNAMIC.
Scheduling Priority
In POSIX 1003.1c-compliant systems, support for the specification of thread priority is optional. The Threads Module determines whether scheduling priority is supported by testing for the definition of macro _POSIX_THREAD_PRIORITY_SCHEDULING.
The Linux POSIX implementation does support system-scope priority scheduling. The Threads Module uses the standard POSIX.1b (formerly POSIX.4) functions sched_get_priority_min() and sched_get_priority_max() to determine the legal range of priority values. Under this implementation, the priorities vary according to the scheduling policy, as shown in Table 8.
Table 8 – Linux with POSIX 1003.1c: Scheduling policy priority values
Scheduling Policy
Minimum Priority
Maximum Priority
Default Priority
RW_THR_PREEMPTIVE
1
99
1
RW_THR_TIME_SLICED_FIXED
1
99
1
RW_THR_TIME_SLICED_DYNAMIC
0
0
0
A thread attribute reports a priority of 0, unless a priority has been explicitly set. Once a thread has been started, the priority of that thread is set to the default value specified in the table above.
Process-scope priority is not supported in the Linux implementation of the Threads Module because Linux POSIX does not support process-scope threads (see “GNU LinuxThreads”).
Scheduling Time-Slice Quantum
The Linux POSIX implementation of the Threads Module does not support the time-slice quantum attribute. Any attempt to get or set this attribute value results in an exception.
Stack Attributes
In POSIX 1003.1c-compliant systems, support for user specification of stack attributes is optional. Linux POSIX supports control for a system-managed stack, and supports user-managed stacks.
The stacks for threads are allocated high in memory with the “grow on demand” flag set and spaced 2 MB apart. This means that they start off small (about 4 KB) and grow to 2 MB. However, the entire 2 MB space is reserved for the stack, which means that mapping something into a fixed address space could cause later growth of the stack to fail. On our test machine with 256 MB RAM and 256 MB swap space, we found that the maximum number of threads that could be created using only pthread_create() was about 230 threads.
The creators of Linux POSIX recommend that you not set stack attributes unless you have strong reasons for doing so, since they believe that the above stack allocation strategy is nearly optimal, and that resetting the stack size may result in nonportable and less reliable programs.