-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.c
More file actions
78 lines (32 loc) · 1.99 KB
/
priority.c
File metadata and controls
78 lines (32 loc) · 1.99 KB
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
/* Angshuman Ghosh 2017CS01 Date: 14/10/2017
Creates a new thread with Round Robin scheduler and least priority
and change the priority to maximum from the thread itself
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
void * myfunc(void * v){
int policy, ret;
struct sched_param param;
pthread_getschedparam(pthread_self(), &policy, ¶m); // get the current scheduler settings for thread
printf(" Existing settings : policy= %s, priority= %d\n", (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "UNKNOWN", param.sched_priority);
printf(" Max priority of %s is %d\n", policy == SCHED_OTHER ? "SCHED_OTHER" : policy == SCHED_FIFO ? "SCHED_FIFO" : "SCHED_RR", sched_get_priority_max(policy));
param.sched_priority = sched_get_priority_max(policy); // Get the maximum possible priority value for the corresponding policy
pthread_setschedparam(pthread_self(), policy, ¶m); // Set the new priority value for the policy
printf("After changing ... policy = %s priority = %d \n", (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "UNKNOWN", param.sched_priority);
return 0;
}
int main(){
int r,policy;
pthread_t thread; // thread
pthread_attr_t attr; // thread attribute
struct sched_param param; // struct to store priority value
pthread_attr_init(&attr); // initialize the attributes
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); // set scheduling to explicit so that it does not invoke SCHED_OTHER as default
param.sched_priority = sched_get_priority_min(SCHED_RR); // set scheduling priority of thread as minimum for RR
pthread_attr_setschedpolicy(&attr, SCHED_RR); // set the Round Robin scheduler
pthread_attr_setschedparam(&attr, ¶m); // set the parameters -> priority
pthread_create(&thread, &attr, &myfunc, NULL); // create thead
pthread_join(thread,NULL);
}