-
Notifications
You must be signed in to change notification settings - Fork 0
/
posix_thread_3priority.c
56 lines (47 loc) · 1.26 KB
/
posix_thread_3priority.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h> // for EPERM
/*
gcc [file] -lpthread
*/
int main(void)
{
int ret;
struct sched_param param_set;
struct sched_param param_get;
pthread_t this_thread;
int policy;
this_thread = pthread_self();
// before
ret = pthread_getschedparam(this_thread, &policy, ¶m_get);
if (ret != 0)
{
printf("pthread_getschedparam : failed\n");
return 0;
}
printf ("before: policy=%ld, priority=%ld\n", policy, param_get.sched_priority);
// highest
param_set.sched_priority = sched_get_priority_max(SCHED_FIFO);
printf("highest priority = %d\n", param_set.sched_priority);
// set
ret = pthread_setschedparam(this_thread, SCHED_FIFO, ¶m_set);
if (ret == EPERM) {
printf("pthread_setschedparam : failed because non-root user\n");
return 0;
}
if (ret != 0)
{
printf("pthread_setschedparam : failed\n");
return 0;
}
// after
ret = pthread_getschedparam(this_thread, &policy, ¶m_get);
if (ret != 0)
{
printf("pthread_getschedparam : failed\n");
return 0;
}
printf ("after: policy=%ld, priority=%ld\n", policy, param_get.sched_priority);
return 0;
}