Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Threads Module Platform Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

4.3 Linux POSIX 1003.1c Thread Attribute Support

This section describes the Linux POSIX 1003.1c thread attribute support.

4.3.1 Scheduling Attributes

This section describes the Linux POSIX 1003.1c implementation-specific support, behavior, and restrictions for thread scheduling attributes.

4.3.1.1 Start Policy

The start policy attribute is fully supported by the Linux implementation of the Threads Module and defaults to RW_THR_START_RUNNING.

4.3.1.2 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 Section 4.4). 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.

4.3.1.3 Scheduling Inheritance Policy

The Linux POSIX implementation of the Threads Module fully supports the scheduling inheritance policy attribute, which defaults to RW_THR_INHERIT.

4.3.1.4 Concurrency Policy

Linux does not support process-scope threads because each Linux thread is a separate Unix process (see Section 4.4). 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.

4.3.1.5 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:

Note that Linux POSIX limits the scheduling policies SCHED_RR and SCHED_FIFO to processes with superuser privileges. Therefore, the RWSchedulingPolicy values 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.

4.3.1.6 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 Section 4.4).

4.3.1.7 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.

4.3.2 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.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.