push
[des2015.git] / natanael / ex03 / ex03b.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 #define ITER 10
13
14 static RT_TASK t1;
15 static RT_TASK t2;
16
17 int global = 0;
18
19 RT_SEM semGlobal1;
20 RT_SEM semGlobal2;
21
22 void taskOne(void *arg)
23 {
24 int i;
25 for (i=0; i < ITER; i++)
26 {
27 //rt_printf("Before semGlobal2..............\n");
28 rt_sem_p(&semGlobal2, 0);
29 rt_printf("I am taskOne and global = %d................\n", ++global);
30 rt_sem_v(&semGlobal1);
31 //rt_printf("After semGlobal1..............\n");
32 }
33 }
34
35 void taskTwo(void *arg)
36 {
37 int i;
38 for (i=0; i < ITER; i++)
39 {
40 //rt_printf("Before semGlobal1--------------\n");
41 rt_sem_p(&semGlobal1, 0);
42 rt_printf("I am taskTwo and global = %d----------------\n", --global);
43 rt_sem_v(&semGlobal2);
44 //rt_printf("After semGlobal2--------------\n");
45 }
46 }
47
48 int main(int argc, char* argv[])
49 {
50 /* Perform auto-init of rt_print buffers if the task doesn't do so */
51 rt_print_auto_init(1);
52
53 /* Avoids memory swapping for this program */
54 mlockall(MCL_CURRENT|MCL_FUTURE);
55
56 /* create semaphore */
57 rt_sem_create(&semGlobal1,"semaphore1",0,S_FIFO);
58 rt_sem_create(&semGlobal2,"semaphore2",1,S_FIFO);
59
60 /* create the two tasks */
61 rt_task_create(&t1, "task1", 0, 1, 0);
62 rt_task_create(&t2, "task2", 0, 1, 0);
63
64 /* start the two tasks */
65 rt_task_start(&t1, &taskOne, 0);
66 rt_task_start(&t2, &taskTwo, 0);
67
68 return 0;
69 }
70