-
Notifications
You must be signed in to change notification settings - Fork 2
/
lab15.c
107 lines (78 loc) · 2.46 KB
/
lab15.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
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/timer.h>
#include <native/sem.h>
#include <rtdk.h>
#include <sys/io.h>
#define NTASKS 3
RT_TASK demo_task[NTASKS];
RT_SEM mysync;
// set new baseperiod for the clock :
// - clock only increments counter when a base period is passed
// - the unit of the clock counter is called a jiffie, which in fact
// represents that a baseperiod is passed
// e.g. If 10 baseperiods are passed, the clock gives an increase
// in 10 jiffies
#define BASEPERIOD 1e6 // baseperiod in ns.
// when base period is set, all times in the api are expressed in jiffies
#define EXECTIME 200 // execution time in jiffies
#define SPINTIME 10 // spin time in jiffies
void demo(void *arg)
{
RTIME starttime, runtime;
int num=*(int *)arg;
RT_TASK *curtask;
RT_TASK_INFO curtaskinfo;
rt_printf("Task : %d\n",num);
rt_sem_p(&mysync,TM_INFINITE);
rt_task_set_mode(0,T_RRB ,NULL);
// let the task run RUNTIME(=200) jiffies in steps of SPINTIME(=20) jiffies
runtime = 0;
while(runtime < EXECTIME) {
rt_timer_spin(SPINTIME*BASEPERIOD); // spin cpu doing nothing
// note: rt_timer_spin function does not accept jiffies only nanoseconds
// deviates from timing conventions throughout the Xenomai API
runtime = runtime + SPINTIME;
rt_printf("Running Task : %d at time : %d\n",num,runtime);
}
rt_printf("End Task : %d\n",num);
}
//startup code
void startup()
{
int i;
char str[10] ;
// semaphore to sync task startup on
rt_sem_create(&mysync,"MySemaphore",0,S_FIFO);
// change to period mode because round robin does not work
// in one shot mode
rt_timer_set_mode(BASEPERIOD);// set tick period
for(i=0; i < NTASKS; i++) {
rt_printf("start task : %d\n",i);
sprintf(str,"task%d",i);
rt_task_create(&demo_task[i], str, 0, 50, 0);
rt_task_slice(&demo_task[i], 5);
rt_task_start(&demo_task[i], &demo, &i);
}
rt_printf("wake up all tasks\n");
rt_sem_broadcast(&mysync);
}
void init_xenomai() {
/* Avoids memory swapping for this program */
mlockall(MCL_CURRENT|MCL_FUTURE);
/* Perform auto-init of rt_print buffers if the task doesn't do so */
rt_print_auto_init(1);
}
int main(int argc, char* argv[])
{
printf("\nType CTRL-C to end this program\n\n" );
// code to set things to run xenomai
init_xenomai();
//startup code
startup();
// wait for CTRL-c is typed to end the program
pause();
}