-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinheritance.c
executable file
·109 lines (89 loc) · 2.81 KB
/
inheritance.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//#define _GNU_SOURCE
//#define __USE_UNIX98 /* Needed for PTHREAD_PRIO_INHERIT */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
static pthread_t t1, t2, t3; //declare threads
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // mutex variable
static pthread_mutexattr_t mta; // mutex attribute variable
struct sched_param param; //declare structure variable for buffering thread priority
int policy = SCHED_RR;
int priority = 10;
void function()
{
//do some work here.
}
void *thread1handler()
{
pthread_mutex_lock(&mutex);
//function 1 for thread 1.
printf("Thread 1 created.\n");
printf("Locked thread 1\n");
//do some work here
int i = 0;
for(i = 0; i < 100000000; i++) {
//printf("1\n");
}
printf("Released thread 1\n");
printf("Thread 1 out\n");
pthread_mutex_unlock(&mutex);
//pthread_exit(NULL);
}
void *thread2handler()
{
//function 2 for thread 2.
printf("Thread 2 created.\n");
//do some work here and call function()
int i = 0;
for(i = 0; i < 10000000; i++) {
//printf("2\n");
}
printf("Thread 2 out\n");
//pthread_exit(NULL);
}
void *thread3handler()
{
pthread_mutex_lock(&mutex);
//function 3 for thread 3.
printf("Thread 3 created.\n");
printf("Locked thread 3.\n");
//do some work here
int i = 0;
for(i = 0; i < 5; i++) {
//printf("3\n");
}
printf("Released thread 3.\n");
printf("Thread 3 out\n");
pthread_mutex_unlock(&mutex);
//pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
//create mutex and initialize it.
pthread_mutexattr_init(&mta);
//set protocol for mutex attribute
pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT);
//initialize mutex with attribute
pthread_mutex_init(&mutex, &mta);
//create thread 1, thread 2 and thread 3
//set handler for thread 1, thread 2 and thread 3 respectively
int maxpriority = sched_get_priority_max(policy);
int minpriority = sched_get_priority_min(policy);
pthread_create(&t1, NULL, (void *) thread1handler, NULL);0
//set different priority for each thread
param.sched_priority = minpriority;
pthread_setschedparam(t1, policy, ¶m); // thread 1 lowest
pthread_create(&t3, NULL, (void *) thread3handler, NULL);
param.sched_priority = maxpriority;
pthread_setschedparam(t3, policy, ¶m); // thread 3 high priority
sleep(1);
pthread_create(&t2, NULL, (void *) thread2handler, NULL);
param.sched_priority = priority;
pthread_setschedparam(t2, policy, ¶m);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
return 0;
}