-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment1.cpp
440 lines (351 loc) · 12.8 KB
/
Assignment1.cpp
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
//compile with: g++ -lpthread <sourcename> -o <executablename>
//This exercise shows how to schedule threads with Rate Monotonic
#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 task4_code( );
//code of aperiodic tasks (if any)
//characteristic function of the thread, only for timing and synchronization
//periodic tasks
void *task1( void *);
void *task2( void *);
void *task3( void *);
void *task4( void *);
//aperiodic tasks (if any)
//Global Variables
int T1T2;
int T1T4;
int T2T3;
// initialization of mutexes and conditions (only for aperiodic scheduling)
static pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mymutex3 = PTHREAD_MUTEX_INITIALIZER;
#define INNERLOOP 5
#define OUTERLOOP 15
#define NPERIODICTASKS 4
#define NAPERIODICTASKS 0
#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 80 millisecond
//the second task has period 100 millisecond
//the third task has period 160 millisecond
//the third task has period 200 millisecond
//you can already order them according to their priority;
//if not, you will need to sort them
periods[0]= 80000000; //in nanoseconds
periods[1]= 100000000; //in nanoseconds
periods[2]= 160000000; //in nanoseconds
periods[3]= 200000000; //in nanoseconds
//for aperiodic tasks we set the period equals to 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++)
{
// initializa 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)
task4_code();
//aperiodic tasks
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++)
{
//initializa 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;
//set the attributes and the parameters of the current thread (pthread_attr_setschedparam)
pthread_attr_setschedparam(&(attributes[i]), &(parameters[i]));
}
// aperiodic tasks
//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;
}
// We should create three mutexes for the three global variables to protect the critical sections
// Each Mutex will protect each global variable
pthread_mutexattr_t mymutexattr;
pthread_mutexattr_init(&mymutexattr);
pthread_mutexattr_setprotocol(&mymutexattr, PTHREAD_PRIO_PROTECT);
pthread_mutexattr_setprioceiling(&mymutexattr, parameters[0].sched_priority);
pthread_mutex_init(&mymutex1, &mymutexattr);
pthread_mutexattr_setprioceiling(&mymutexattr, parameters[0].sched_priority);
pthread_mutex_init(&mymutex2, &mymutexattr);
pthread_mutexattr_setprioceiling(&mymutexattr, parameters[1].sched_priority);
pthread_mutex_init(&mymutex2, &mymutexattr);
pthread_mutexattr_destroy(&mymutexattr);
// 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]), task4, 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++)
{
// Locking up the mutex 1
pthread_mutex_lock( &mymutex1 );
T1T2 = 2;
// Unlocking up the mutex 1
pthread_mutex_unlock( &mymutex1 );
// Locking up the mutex 2
pthread_mutex_lock( &mymutex2 );
T1T4=4;
// Unlocking up the mutex 2
pthread_mutex_unlock( &mymutex2 );
}
}
// when the random variable uno=0, then aperiodic task 5 must
// be executed
// when the random variable uno=1, then aperiodic task 5 must
// be executed
//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);
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;
}
}
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++)
{
// Lock mutex
pthread_mutex_lock( &mymutex1 );
printf("%d", T1T2);
//Unlock mutex
pthread_mutex_unlock( &mymutex1 );
// Lock mutex
pthread_mutex_lock( &mymutex3 );
T2T3=6;
//Unlock mutex
pthread_mutex_unlock( &mymutex3 );
}
}
//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;
}
}
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++)
{
// Lock mutex
pthread_mutex_lock( &mymutex3 );
printf("%d", T2T3);
//Unlock mutex
pthread_mutex_unlock( &mymutex3 );
}
}
//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;
}
}
void task4_code()
{
//print the id of the current task
printf(" 4[ "); fflush(stdout);
int i,j;
double uno;
for (i = 0; i < OUTERLOOP; i++)
{
for (j = 0; j < INNERLOOP; j++)
{
// Lock mutex
pthread_mutex_lock( &mymutex2 );
printf("%d", T1T4);
//Unlock mutex
pthread_mutex_unlock( &mymutex2 );
}
}
//print the id of the current task
printf(" ]4 "); fflush(stdout);
}
void *task4( 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++)
{
task4_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;
}
}