update ex11
[des2015.git] / mart / ex06 / ex06a.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5
6 #include <native/task.h>
7 #include <native/timer.h>
8 #include <native/sem.h>
9
10 #include <rtdk.h>
11 #include <sys/io.h>
12
13 #define NTASKS 3
14
15 #define BASEPERIOD 0 // baseperiod 0 to get ns
16
17 #define EXECTIME 2e8 // execution time in ns
18 #define SPINTIME 1e7 // spin time in ns
19
20 RT_TASK demo_task[NTASKS];
21 RT_SEM mysync;
22
23 void demo(void *arg)
24 {
25 RTIME starttime;
26 RTIME runtime;
27 int num = *(int *)arg;
28 RT_TASK *curtask;
29 RT_TASK_INFO curtaskinfo;
30
31 rt_printf("Task: %d\n", num);
32
33 rt_sem_p(&mysync, TM_INFINITE);
34
35 runtime = 0;
36 while(runtime < EXECTIME) {
37 rt_timer_spin(SPINTIME); // spin cpu doing nothing
38 runtime = runtime + SPINTIME;
39 rt_printf("Running Task: %d at ms: %d\n", num, runtime/1000000);
40 }
41 rt_printf("End Task: %d\n", num);
42 }
43
44 //startup code
45 void startup()
46 {
47 int i;
48 char str[10] ;
49
50 // semaphore to sync task startup on
51 rt_sem_create(&mysync, "MySemaphore", 0, S_FIFO);
52
53 // set timing to ns
54 rt_timer_set_mode(BASEPERIOD);
55
56 for(i = 0; i < NTASKS; i++) {
57 rt_printf("start task: %d\n",i);
58 sprintf(str,"task%d",i);
59 rt_task_create(&demo_task[i], str, 0, 50+i, 0);
60 rt_task_start(&demo_task[i], &demo, &i);
61 }
62
63 rt_printf("wake up all tasks\n");
64 rt_sem_broadcast(&mysync);
65 }
66
67 void init_xenomai() {
68 /* Avoids memory swapping for this program */
69 mlockall(MCL_CURRENT|MCL_FUTURE);
70
71 /* Perform auto-init of rt_print buffers if the task doesn't do so */
72 rt_print_auto_init(1);
73 }
74
75 int main(int argc, char* argv[])
76 {
77 printf("\nType CTRL-C to end this program\n\n" );
78
79 // code to set things to run xenomai
80 init_xenomai();
81
82 //startup code
83 startup();
84
85 // wait for CTRL-c is typed to end the program
86 pause();
87 }