8932358e6ecd310ef2a3273efd6a12a3a89d5513
[des2015.git] / mart / ex08 / ex08a.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
12 static RT_TASK tl;
13 static RT_TASK tm;
14 static RT_TASK th;
15 RTIME timeblock = 1e7;
16 RT_SEM sem;
17
18 int global = 0;
19
20 void t_low(void *arg)
21 {
22 int i;
23 for(i = 0; i<3; i++){
24 rt_sem_p(&sem, TM_INFINITE);
25 rt_printf("Low priority task locks semaphore\n");
26 rt_task_sleep(timeblock*3);
27 rt_printf("Low priority task unlocks semaphore\n");
28 rt_sem_v(&sem);
29 }
30 rt_printf(".....Low priority task ends\n");
31 }
32
33 void t_med(void *arg)
34 {
35 rt_task_sleep(timeblock*2);
36 int i;
37 for(i = 0; i<10; i++){
38 rt_printf("Medium task running\n");
39 rt_timer_spin(timeblock);
40 }
41 rt_printf(".....Medium priority task ends\n");
42 }
43
44 void t_high(void *arg)
45 {
46 rt_task_sleep(timeblock*3);
47 int i;
48 for(i = 0; i<3; i++){
49 rt_printf("High priority task tries to locks semaphore\n");
50 rt_sem_p(&sem, TM_INFINITE);
51 rt_printf("High priority task locks semaphore\n");
52 rt_timer_spin(timeblock*3);
53 rt_printf("High priority task unlocks semaphore\n");
54 rt_sem_v(&sem);
55 }
56 rt_printf(".....High priority task ends\n");
57 }
58
59 int main(int argc, char* argv[])
60 {
61 /* Perform auto-init of rt_print buffers if the task doesn't do so */
62 rt_print_auto_init(1);
63
64 /* Avoids memory swapping for this program */
65 mlockall(MCL_CURRENT | MCL_FUTURE);
66
67 /* create the two tasks */
68 rt_task_create(&tl, "t_low", 0, 49, 0);
69 rt_task_create(&tm, "t_med", 0, 50, 0);
70 rt_task_create(&th, "t_high", 0, 51, 0);
71
72 /* create semaphore and start in blocked mode */
73 rt_sem_create(&sem, "sem", 1, S_PRIO);
74
75 /* start the two tasks */
76 rt_task_start(&tl, &t_low, 0);
77 rt_task_start(&tm, &t_med, 0);
78 rt_task_start(&th, &t_high, 0);
79
80 pause();
81 }