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
',
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
',
42 self->index,
43 (my_policy == SCHED_FIFO ? 'FIFO'
44 : (my_policy == SCHED_RR ? 'RR'
45 : (my_policy == SCHED_OTHER ? 'OTHER' 46 : 'unknown'))),
47 my_param.sched_priority);
48 return NULL;
49 }
50
51 int main (int argc, char *argv[])
52 {
53 int count, status;
54
55 rr_min_priority = sched_get_priority_min (SCHED_RR);
56 if (rr_min_priority == -1) {
57 #ifdef sun
58 if (errno == ENOSYS) {
59 fprintf (stderr, 'SCHED_RR is not supported.
');
60 exit (0);
61 }
62 #endif
63 errno_abort ('Get SCHED_RR min priority');
64 }
65 for (count = 0; count < THREADS; count++) {
66 threads[count].index = count;
67 status = pthread_create (
68 &threads[count].id, NULL,
69 thread_routine, (void*)&threads[count]);
70 if (status != 0)
71 err_abort (status, 'Create thread');
72 }
73 for (count = 0; count < THREADS; count++) {
74 status = pthread_join (threads[count].id, NULL);
75 if (status != 0)
76 err_abort (status, 'Join thread');
77 }
78 printf ('Main exiting
');
79 return 0;
80 }
5.5.3 Contention scope and allocation domain
int pthread_attr_getscope (
const pthread_attr_t *attr, int *contentionscope);
int pthread_attr_setscope (
pthread_attr_t *attr, int contentionscope);
Besides scheduling policy and parameters, two other controls are important in realtime scheduling. Unless you are writing a realtime application, they probably don't matter. If you are writing a realtime application, you will need to find out which settings of these controls are supported by a system.
The first control is called
Pthreads provides the thread pthread_attr_setscope
will return ENOTSUP.
The second control is