-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRM_aperiodic_background.c
531 lines (446 loc) · 15.9 KB
/
RM_aperiodic_background.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//compile with: g++ -lpthread <sourcename> -o <executablename>
//This exercise show how to schedule threads with Rate Monotonic with aperiodic tasks in background
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/types.h>
//code of periodic tasks
void task1_code( );
void task2_code( );
void task3_code( );
void task_per_code();
//code of aperiodic tasks
void task4_code( );
void task5_code( );
void task_aper_code();
//characteristic function of the thread, only for timing and synchronization
//periodic tasks
void *task1( void *);
void *task2( void *);
void *task3( void *);
void *task_per( void *);
//aperiodic tasks
void *task4( void *);
void *task5( void *);
void *task_aper( void *);
// initialization of mutexes and conditions (only for aperiodic scheduling)
pthread_mutex_t mutex_task_4 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_task_5 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_task_aper = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_task_4 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_task_5 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_task_aper = PTHREAD_COND_INITIALIZER;
#define INNERLOOP 1000
#define OUTERLOOP 100
#define NPERIODICTASKS 4
#define NAPERIODICTASKS 3
#define NTASKS NPERIODICTASKS + NAPERIODICTASKS
long int periods[NTASKS];
struct timespec next_arrival_time[NTASKS];
double WCET[NTASKS];
pthread_attr_t attributes[NTASKS];
pthread_t thread_id[NTASKS];
struct sched_param parameters[NTASKS];
int missed_deadlines[NTASKS];
int
main()
{
// set task periods in nanoseconds
//the first task has period 100 milliseconds
//the second task has period 200 milliseconds
//the third task has period 400 milliseconds
//the fourth task has a period of 500 milliseconds
//you can already order them according to their priority;
//if not, you will need to sort them
periods[0]= 100000000; //in nanoseconds
periods[1]= 200000000; //in nanoseconds
periods[2]= 400000000; //in nanoseconds
periods[3]= 500000000; // for the periodic task
//for aperiodic tasks we set the period equals to 0
periods[4]= 0;
periods[5]= 0;
periods[6]= 0;
//this is not strictly necessary, but it is convenient to
//assign a name to the maximum and the minimum priotity in the
//system. We call them priomin and priomax.
struct sched_param priomax;
priomax.sched_priority=sched_get_priority_max(SCHED_FIFO);
struct sched_param priomin;
priomin.sched_priority=sched_get_priority_min(SCHED_FIFO);
// set the maximum priority to the current thread (you are required to be
// superuser). Check that the main thread is executed with superuser privileges
// before doing anything else.
if (getuid() == 0)
pthread_setschedparam(pthread_self(),SCHED_FIFO,&priomax);
// execute all tasks in standalone modality in order to measure execution times
// (use gettimeofday). Use the computed values to update the worst case execution
// time of each task.
int i;
for (i =0; i < NTASKS; i++)
{
// initialize time_1 and time_2 required to read the clock
struct timespec time_1, time_2;
clock_gettime(CLOCK_REALTIME, &time_1);
//we should execute each task more than one for computing the WCET
//periodic tasks
if (i==0)
task1_code();
if (i==1)
task2_code();
if (i==2)
task3_code();
if (i==3)
task_per_code();
//aperiodic tasks
if (i==4)
task4_code();
if (i==5)
task5_code();
if (i==6)
task_aper_code();
clock_gettime(CLOCK_REALTIME, &time_2);
// compute the Worst Case Execution Time (in a real case, we should repeat this many times under
//different conditions, in order to have reliable values
WCET[i]= 1000000000*(time_2.tv_sec - time_1.tv_sec)
+(time_2.tv_nsec-time_1.tv_nsec);
printf("\nWorst Case Execution Time %d=%f \n", i, WCET[i]);
}
// compute U
double U = WCET[0]/periods[0]+WCET[1]/periods[1]+WCET[2]/periods[2]+WCET[3]/periods[3];
// compute Ulub by considering the fact that we have harmonic relationships between periods
double Ulub = 1;
//if there are no harmonic relationships, use the following formula instead
//double Ulub = NPERIODICTASKS*(pow(2.0,(1.0/NPERIODICTASKS)) -1);
//check the sufficient conditions: if they are not satisfied, exit
if (U > Ulub)
{
printf("\n U=%lf Ulub=%lf Non schedulable Task Set", U, Ulub);
return(-1);
}
printf("\n U=%lf Ulub=%lf Scheduable Task Set", U, Ulub);
fflush(stdout);
sleep(5);
// set the minimum priority to the current thread: this is now required because
//we will assign higher priorities to periodic threads to be soon created
//pthread_setschedparam
if (getuid() == 0)
pthread_setschedparam(pthread_self(),SCHED_FIFO,&priomin);
// set the attributes of each task, including scheduling policy and priority
for (i = 0; i < NPERIODICTASKS; i++)
{
//initialize the attribute structure of task i
pthread_attr_init(&(attributes[i]));
//set the attributes to tell the kernel that the priorities and policies are explicitly chosen,
//not inherited from the main thread (pthread_attr_setinheritsched)
pthread_attr_setinheritsched(&(attributes[i]), PTHREAD_EXPLICIT_SCHED);
// set the attributes to set the SCHED_FIFO policy (pthread_attr_setschedpolicy)
pthread_attr_setschedpolicy(&(attributes[i]), SCHED_FIFO);
//properly set the parameters to assign the priority inversely proportional
//to the period
//parameters[i].sched_priority = priomin.sched_priority+NTASKS - i;
parameters[i].sched_priority = sched_get_priority_max(SCHED_FIFO) - i;
//set the attributes and the parameters of the current thread (pthread_attr_setschedparam)
pthread_attr_setschedparam(&(attributes[i]), &(parameters[i]));
}
// aperiodic tasks
for (int i = NPERIODICTASKS; i < NTASKS; i++)
{
pthread_attr_init(&(attributes[i]));
pthread_attr_setschedpolicy(&(attributes[i]), SCHED_FIFO);
//set minimum priority (background scheduling)
parameters[i].sched_priority = 0;
pthread_attr_setschedparam(&(attributes[i]), &(parameters[i]));
}
//delare the variable to contain the return values of pthread_create
int iret[NTASKS];
//declare variables to read the current time
struct timespec time_1;
clock_gettime(CLOCK_REALTIME, &time_1);
// set the next arrival time for each task. This is not the beginning of the first
// period, but the end of the first period and beginning of the next one.
for (i = 0; i < NPERIODICTASKS; i++)
{
long int next_arrival_nanoseconds = time_1.tv_nsec + periods[i];
//then we compute the end of the first period and beginning of the next one
next_arrival_time[i].tv_nsec= next_arrival_nanoseconds%1000000000;
next_arrival_time[i].tv_sec= time_1.tv_sec + next_arrival_nanoseconds/1000000000;
missed_deadlines[i] = 0;
}
// create all threads(pthread_create)
iret[0] = pthread_create( &(thread_id[0]), &(attributes[0]), task1, NULL);
iret[1] = pthread_create( &(thread_id[1]), &(attributes[1]), task2, NULL);
iret[2] = pthread_create( &(thread_id[2]), &(attributes[2]), task3, NULL);
iret[3] = pthread_create( &(thread_id[3]), &(attributes[3]), task_per, NULL);
iret[4] = pthread_create( &(thread_id[4]), &(attributes[4]), task4, NULL);
iret[5] = pthread_create( &(thread_id[5]), &(attributes[5]), task5, NULL);
iret[6] = pthread_create( &(thread_id[6]), &(attributes[6]), task_aper, NULL);
// join all threads (pthread_join)
pthread_join( thread_id[0], NULL);
pthread_join( thread_id[1], NULL);
pthread_join( thread_id[2], NULL);
pthread_join( thread_id[3], NULL);
// set the next arrival time for each task. This is not the beginning of the first
// period, but the end of the first period and beginning of the next one.
for (i = 0; i < NTASKS; i++) {
printf ("\nMissed Deadlines Task %d=%d", i, missed_deadlines[i]);
fflush(stdout);
}
exit(0);
}
// application specific task_1 code
void task1_code()
{
//print the id of the current task
printf(" 1[ "); fflush(stdout);
//this double loop with random computation is only required to waste time
int i,j;
double uno;
for (i = 0; i < OUTERLOOP; i++)
{
for (j = 0; j < INNERLOOP; j++)
{
uno = rand()*rand()%10;
}
}
// when the random variable uno=0, then aperiodic task 5 must
// be executed
if (uno == 0) {
printf(":ex(4)");fflush(stdout);
// In theory, we should protect conditions using mutexes. However, in a real-time application, something undesirable may happen.
// Indeed, when task1 takes the mutex and sends the condition, task4 is executed and is given the mutex by the kernel. Which means
// that task1 (higher priority) would be blocked waiting for task4 to finish (lower priority). This is of course unacceptable,
// as it would produced a priority inversion. For this reason, we are not putting mutexes here. A better solution should be found.
// pthread_mutex_lock(&mutex_task_4);
pthread_cond_signal(&cond_task_4);
// pthread_mutex_unlock(&mutex_task_4);
}
// when the random variable uno=1, then aperiodic task 5 must
// be executed
if (uno == 1)
{
printf(":ex(5)");fflush(stdout);
// See below why mutexes have been commented
// pthread_mutex_lock(&mutex_task_5);
pthread_cond_signal(&cond_task_5);
// pthread_mutex_unlock(&mutex_task_5);
}
if (uno == 2)
{
printf(":ex(aper)");fflush(stdout);
// See below why mutexes have been commented
// pthread_mutex_lock(&mutex_task_5);
pthread_cond_signal(&cond_task_aper);
// pthread_mutex_unlock(&mutex_task_5);
}
//print the id of the current task
printf(" ]1 "); fflush(stdout);
}
//thread code for task_1 (used only for temporization)
void *task1( void *ptr)
{
// set thread affinity, that is the processor on which threads shall run
cpu_set_t cset;
CPU_ZERO (&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
//execute the task one hundred times... it should be an infinite loop (too dangerous)
int i=0;
for (i=0; i < 100; i++)
{
// execute application specific code
task1_code();
// it would be nice to check if we missed a deadline here... why don't
// you try by yourself?
// sleep until the end of the current period (which is also the start of the
// new one
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_arrival_time[0], NULL);
// the thread is ready and can compute the end of the current period for
// the next iteration
long int next_arrival_nanoseconds = next_arrival_time[0].tv_nsec + periods[0];
next_arrival_time[0].tv_nsec= next_arrival_nanoseconds%1000000000;
next_arrival_time[0].tv_sec= next_arrival_time[0].tv_sec + next_arrival_nanoseconds/1000000000;
}
return NULL;
}
void task2_code()
{
//print the id of the current task
printf(" 2[ "); fflush(stdout);
int i,j;
double uno;
for (i = 0; i < OUTERLOOP; i++)
{
for (j = 0; j < INNERLOOP; j++)
{
uno = rand()*rand()%10;
}
}
//print the id of the current task
printf(" ]2 "); fflush(stdout);
}
void *task2( void *ptr )
{
// set thread affinity, that is the processor on which threads shall run
cpu_set_t cset;
CPU_ZERO (&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
int i=0;
for (i=0; i < 100; i++)
{
task2_code();
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_arrival_time[1], NULL);
long int next_arrival_nanoseconds = next_arrival_time[1].tv_nsec + periods[1];
next_arrival_time[1].tv_nsec= next_arrival_nanoseconds%1000000000;
next_arrival_time[1].tv_sec= next_arrival_time[1].tv_sec + next_arrival_nanoseconds/1000000000;
}
return NULL;
}
void task3_code()
{
//print the id of the current task
printf(" 3[ "); fflush(stdout);
int i,j;
double uno;
for (i = 0; i < OUTERLOOP; i++)
{
for (j = 0; j < INNERLOOP; j++);
double uno = rand()*rand()%10;
}
//print the id of the current task
printf(" ]3 "); fflush(stdout);
}
void *task3( void *ptr)
{
// set thread affinity, that is the processor on which threads shall run
cpu_set_t cset;
CPU_ZERO (&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
int i=0;
for (i=0; i < 100; i++)
{
task3_code();
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_arrival_time[2], NULL);
long int next_arrival_nanoseconds = next_arrival_time[2].tv_nsec + periods[2];
next_arrival_time[2].tv_nsec= next_arrival_nanoseconds%1000000000;
next_arrival_time[2].tv_sec= next_arrival_time[2].tv_sec + next_arrival_nanoseconds/1000000000;
}
return NULL;
}
void task4_code()
{
printf(" 4[ "); fflush(stdout);
for (int i = 0; i < OUTERLOOP; i++)
{
for (int j = 0; j < INNERLOOP; j++)
double uno = rand()*rand();
}
printf(" ]4 "); fflush(stdout);
fflush(stdout);
}
void *task4( void *)
{
// set thread affinity, that is the processor on which threads shall run
cpu_set_t cset;
CPU_ZERO (&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
//add an infinite loop
while (1)
{
// wait for the proper condition to be signalled
// See below why mutexes have been commented
// pthread_mutex_lock(&mutex_task_4);
pthread_cond_wait(&cond_task_4, &mutex_task_4);
// pthread_mutex_unlock(&mutex_task_4);
// execute the task code
task4_code();
}
}
void task5_code()
{
printf(" 5[ "); fflush(stdout);
for (int i = 0; i < OUTERLOOP; i++)
{
for (int j = 0; j < INNERLOOP; j++)
double uno = rand()*rand();
}
printf(" ]5 "); fflush(stdout);
}
void *task5( void *)
{
// set thread affinity, that is the processor on which threads shall run
cpu_set_t cset;
CPU_ZERO (&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
//add an infinite loop
while(1)
{
// wait for the proper condition to be signalled
// See below why mutexes have been commented
// pthread_mutex_lock(&mutex_task_5);
pthread_cond_wait(&cond_task_5, &mutex_task_5);
// pthread_mutex_unlock(&mutex_task_5);
// execute the task code
task5_code();
}
}
void task_per_code()
{
printf("per[ "); fflush(stdout);
for (int i = 0; i < OUTERLOOP; i++)
{
for (int j = 0; j < INNERLOOP; j++)
{
double uno = rand() * rand() % 10;
}
}
printf(" ]per "); fflush(stdout);
}
void *task_per(void *ptr)
{
cpu_set_t cset;
CPU_ZERO(&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
for (int i = 0; i < 100; i++)
{
task_per_code();
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_arrival_time[3], NULL);
long int next_arrival_nanoseconds = next_arrival_time[3].tv_nsec + periods[3];
next_arrival_time[3].tv_nsec = next_arrival_nanoseconds % 1000000000;
next_arrival_time[3].tv_sec = next_arrival_time[3].tv_sec + next_arrival_nanoseconds / 1000000000;
}
return NULL;
}
void task_aper_code() {
printf(" aper[ "); fflush(stdout);
for (int i = 0; i < OUTERLOOP; i++)
{
for (int j = 0; j < INNERLOOP; j++)
{
double uno = rand() * rand();
}
}
printf(" ]aper "); fflush(stdout);
}
void *task_aper(void *)
{
cpu_set_t cset;
CPU_ZERO(&cset);
CPU_SET(0, &cset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cset);
while (1)
{
//pthread_mutex_lock(&mutex_task_aper);
pthread_cond_wait(&cond_task_aper, &mutex_task_aper);
//pthread_mutex_unlock(&mutex_task_aper);
task_aper_code();
}
}