The pthread_attr_setschedpolicy
and pthread_attr_setschedparam
, specify the explicit scheduling policy and parameters for the attributes object. Pthreads does not specify a default value for either of these attributes, which means that each implementation may choose some "appropriate" value. A realtime operating system intended for embedded controller applications, for example, might choose to create threads by default with SCHED_FIFO policy, and, perhaps, some medium-range priority.
Most multiuser operating systems are more likely to use a nonstandard "time-share" scheduling policy by default, causing threads to be scheduled more or less as processes have always been scheduled. The system may, for example, temporarily reduce the priority of "CPU hogs" so that they cannot prevent other threads from making progress.
One example of a multiuser operating system is Digital UNIX, which supports two nonstandard timeshare scheduling policies. The
When you set the scheduling policy or priority attributes in an attributes object,you must also set the inheritsched attribute!
The pthread_attr_setinheritsched
, controls whether a thread you create inherits scheduling information from the creating thread, or uses the explicit scheduling information in the
Set the
58-118 The following program, sched_attr.c, shows how to use an attributes object to create a thread with an explicit scheduling policy and priority. Notice that it uses conditional code to determine whether the priority scheduling feature of Pthreads is supported at compilation time. It will print a message if the option is not supported and continue, although the program in that case will not do much. (It creates a thread with default scheduling behavior, which can only say that it ran.)
Although Solaris 2.5 defines _POSIX_THREAD_PRIORITY_SCHEDULING, it does not support the POSIX realtime scheduling policies, and attempting to set the policy attribute to SCHED_RR would fail. This program treats Solaris as it did not define the _POSIX_THREAD_PRIORITY_SCHEDULING option.
■ sched_attr.c
1 #include
2 #include
3 #include
4 #include "errors.h" 5
6 /*
7 * Thread start routine. If priority scheduling is supported,
8 * report the thread's scheduling attributes.
9 */
10 void *thread_routine (void *arg)
11 {
12 int my_policy;
13 struct sched_param my_param;
14 int status;
15
16 /*
17 * If the priority scheduling option is not defined, then we
18 * can do nothing with the output of pthread_getschedparam,
19 * so just report that the thread ran, and exit.
20 */
21 #if defined (_POSIX_THREAD_PRIORITY_SCHEDULING) && !defined (sun)
22 status = pthread_getschedparam (
23 pthread_self (), &my_policy, &my_param);
24 if (status != 0)
25 err_abort (status, "Get sched");
26 printf ("thread_routine running at %s/%d\n",
27 (my_policy == SCHED_FIFO ? "FIFO"
28 : (my_policy == SCHED_RR ? "RR"
29 : (my_policy == SCHED_OTHER ? "OTHER"
30 : "unknown"))),