-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtpripritydemoc.c
98 lines (64 loc) · 2.32 KB
/
rtpripritydemoc.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
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
//
#define SKED SCHED_FIFO
//#define SKED SCHED_RR
void* looper (void *arg); // prototype
sem_t mysem;
//-----------------------------------
// THREAD PRIORITY DEMO - RTervo Jan 2013
//-----------------------------------
//
//-----------------------------------
main() {
int tid1, tid2, tid3;
pthread_attr_t attr1, attr2, attr3;
struct sched_param param;
pthread_attr_init( &attr1); // assign default attributes
pthread_attr_setinheritsched( &attr1, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr1, SKED );
pthread_attr_init( &attr2); // assign default attributes
pthread_attr_setinheritsched( &attr2, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr2, SKED );
pthread_attr_init( &attr3); // assign default attributes
pthread_attr_setinheritsched( &attr3, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr3, SKED );
int pid = getpid();
printf( "my pid is [%i]\n", pid );
sem_init( &mysem, 1, 3 ); // allow N users
pthread_attr_getschedparam ( &attr1, ¶m );
param.sched_priority= 7; // modify thread priority
pthread_attr_setschedparam ( &attr1, ¶m );
pthread_attr_getschedparam ( &attr2, ¶m );
param.sched_priority= 7; // modify thread priority
pthread_attr_setschedparam ( &attr2, ¶m );
pthread_attr_getschedparam ( &attr3, ¶m );
param.sched_priority= 7; // modify thread priority
pthread_attr_setschedparam ( &attr3, ¶m );
pthread_create( &tid1, &attr1, &looper, (void *) 1 );
pthread_create( &tid2, &attr2, &looper, (void *) 2 );
pthread_create( &tid3, &attr3, &looper, (void *) 3 );
pthread_join(2,NULL);
pthread_join(3,NULL);
pthread_join(4,NULL);
} // end main
//-----------------------------------
void* looper ( void *arg ) {
int tag = (int) arg;
int x = 0;
unsigned int y = 0;
printf( "looper(%i) started\n", tag );
while( x < 10 ) { // do ten loops
sem_wait(&mysem);
printf( "looper(%i) t=%i\n", tag, x++ );
for(y=0;y<0x200000;y++); // loop delay
// sched_yield(); // optional yield
// delay(10); // sleep by msec
sem_post(&mysem);
} // end while
printf( "looper(%i) is done\n", tag );
} // end looper
//-----------------------------------