51 * If the priority scheduling option is defined, set various
52 * scheduling parameters. Note that it is particularly important
53 * that you remember to set the inheritsched attribute to
54 * PTHREAD_EXPLICIT_SCHED, or the policy and priority that you've
55 * set will be ignored! The default behavior is to inherit
56 * scheduling information from the creating thread.
57 */
58 #if defined (_POSIX_THREAD_PRIORITY_SCHEDULING) && !defined (sun)
59 status = pthread_attr_getschedpolicy (
60 &thread_attr, &thread_policy);
61 if (status 1= 0)
62 err_abort (status, 'Get policy');
63 status = pthread_attr_getschedparam (
64 &thread_attr, &thread_param);
65 if (status != 0)
66 err_abort (status, 'Get sched param');
67 printf (
68 'Default policy is %s, priority is %d
',
69 (thread_policy == SCHED_FIFO ? 'FIFO'
70 : (thread_policy == SCHED_RR ? 'RR'
71 : (thread_policy == SCHED_OTHER ? 'OTHER' 72 : 'unknown'))),
73 thread_param.sched_priority);
74
75 status = pthread_attr_setschedpolicy (
76 &thread_attr, SCHED_RR);
77 if (status != 0)
78 printf ('Unable to set SCHED_RR policy.
');
79 else {
80 /*
81 * Just for the sake of the exercise, we'll use the
82 * middle of the priority range allowed for
83 * SCHED_RR. This should ensure that the thread will be
84 * run, without blocking everything else. Because any
85 * assumptions about how a thread's priority interacts
86 * with other threads (even in other processes) are
87 * nonportable, especially on an implementation that
88 * defaults to System contention scope, you may have to
89 * adjust this code before it will work on some systems.
90 */
91 rr_min_priority = sched_get_priority_min (SCHED_RR);
92 if (rr_min_priority == -1)
93 errno_abort ('Get SCHED_RR min priority');
94 rr_max_priority = sched_get_priority_max (SCHED_RR);
95 if (rr_max_priority == -1)
96 errno_abort ('Get SCHED_RR max priority');
97 thread_param.sched_priority =
98 (rr_min_priority + rr_max_priority)/2;
99 printf (
100 'SCHED_RR priority range is %d to %d: using %d
',
101 rr_min_priority,
102 rr_max_priority,
103 thread_param.sched_priority);
104 status = pthread_attr_setschedparam (
105 &thread_attr, &thread_param);
106 if (status != 0)
107 err_abort (status, 'Set params');
108 printf (
109 'Creating thread at RR/%d
',
110 thread_param.sched_priority);
111 status = pthread_attr_setinheritsched (
112 &thread_attr, PTHREAD_EXPLICIT_SCHED);
113 if (status != 0)
114 err_abort (status, 'Set inherit');
115 }
116 #else
117 printf ('Priority scheduling not supported
');
118 #endif
119 status = pthread_create (
120 &thread_id, &thread_attr, thread_routine, NULL);
121 if (status != 0)
122 err_abort (status, 'Create thread');
123 status = pthread_join (thread_id, NULL);
124 if (status != 0)
125 err_abort (status, 'Join thread');
126 printf ('Main exiting
');
127 return 0;
128 }
The next program, sched_thread.c, shows how to modify the realtime scheduling policy and parameters for a running thread. When changing the scheduling policy and parameters in a thread attributes object, remember, you use two separate operations: one to modify the scheduling policy and the other to modify the scheduling parameters.
You cannot modify the scheduling policy of a running thread separately from the thread's parameters, because the policy and parameters must always be consistent for scheduling to operate correctly. Each scheduling policy may have a unique range of valid scheduling priorities, and a thread cannot operate at a priority that isn't valid for its current policy. To ensure consistency of the policy and parameters, they are set with a single call.
55 Unlike sched_attr.c, sched_thread.c does not check the compile-time feature macro _POSIX_THREAD_PRIORITY_SCHEDULING. That means it will probably not compile, and almost certainly won't run correctly, on a system that does not support the option. There's nothing wrong with writing a program that way — in fact, that's what you are likely to do most of the time. If you need priority scheduling, you would document that your application requires the _POSIX_THREAD_ PRIORITY_SCHEDULING option, and use it.
57-62 Solaris 2.5, despite defining _POSIX_THREAD_PRIORITY_SCHEDULING, does not support realtime scheduling policies. For this reason, the ENOSYS from sched_get_priority_min
is handled as a special case.
¦ sched_thread.c
1 #include <unistd.h>
2 #include <pthread.h>
3 #include <sched.h>
4 #include 'errors.h' 5