-
Notifications
You must be signed in to change notification settings - Fork 63
/
sched_thread.c
88 lines (81 loc) · 2.25 KB
/
sched_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* sched_thread.c
*
* Demonstrate dynamic scheduling policy use.
*
* Special note: This demonstration will fail on Solaris 2.5
* because it does not implement SCHED_RR.
*/
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include "errors.h"
#define THREADS 5
/*
* Structure describing each thread.
*/
typedef struct thread_tag {
int index;
pthread_t id;
} thread_t;
thread_t threads[THREADS];
int rr_min_priority;
/*
* Thread start routine that will set its own priority
*/
void *thread_routine (void *arg)
{
thread_t *self = (thread_t*)arg;
int my_policy;
struct sched_param my_param;
int status;
my_param.sched_priority = rr_min_priority + self->index;
DPRINTF ((
"Thread %d will set SCHED_FIFO, priority %d\n",
self->index, my_param.sched_priority));
status = pthread_setschedparam (
self->id, SCHED_RR, &my_param);
if (status != 0)
err_abort (status, "Set sched");
status = pthread_getschedparam (
self->id, &my_policy, &my_param);
if (status != 0)
err_abort (status, "Get sched");
printf ("thread_routine %d running at %s/%d\n",
self->index,
(my_policy == SCHED_FIFO ? "FIFO"
: (my_policy == SCHED_RR ? "RR"
: (my_policy == SCHED_OTHER ? "OTHER"
: "unknown"))),
my_param.sched_priority);
return NULL;
}
int main (int argc, char *argv[])
{
int count, status;
rr_min_priority = sched_get_priority_min (SCHED_RR);
if (rr_min_priority == -1) {
#ifdef sun
if (errno == ENOSYS) {
fprintf (stderr, "SCHED_RR is not supported.\n");
exit (0);
}
#endif
errno_abort ("Get SCHED_RR min priority");
}
for (count = 0; count < THREADS; count++) {
threads[count].index = count;
status = pthread_create (
&threads[count].id, NULL,
thread_routine, (void*)&threads[count]);
if (status != 0)
err_abort (status, "Create thread");
}
for (count = 0; count < THREADS; count++) {
status = pthread_join (threads[count].id, NULL);
if (status != 0)
err_abort (status, "Join thread");
}
printf ("Main exiting\n");
return 0;
}