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\n",
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\n");
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\n");
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
2 #include
3 #include
4 #include "errors.h" 5
6 #define THREADS 5 7
8 /*
9 * Structure describing each thread.
10 */
11 typedef struct thread_tag {
12 int index;
13 pthread_t id;
14 } thread_t;
15
16 thread_t threads[THREADS];
17 int rr_min_priority; 18
19 /*
20 * Thread start routine that willset its own priority.
21 */
22 void *thread_routine (void *arg)
23 {
24 thread_t *self = (thread_t*)arg;
25 int my_policy;
26 struct sched_param my_param;
27 int status;
28
29 my_param.sched_priority = rr_min_priority + self->index;
30 DPRINTF ((
31 "Thread %d will set SCHED_FIFO, priority %d\n",
32 self->index, my_param.sched_priority));
33 status = pthread_setschedparam (
34 self->id, SCHED_RR, &my_param);
35 if (status != 0)
36 err_abort (status, "Set sched");
37 status = pthread_getschedparam (
38 self->id, &my_policy, &my_param);
39 if (status != 0)
40 err_abort (status, "Get sched");
41 printf ("thread_routine %d running at %s/%d\n",