-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingpong-semaphore.c
57 lines (49 loc) · 1.03 KB
/
pingpong-semaphore.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
#include <stdio.h>
#include <stdlib.h>
#include "ppos.h"
task_t a1, a2, b1, b2;
semaphore_t s1, s2 ;
void TaskA (void * arg)
{
int i ;
for (i=0; i<10; i++)
{
sem_down (&s1) ;
printf ("%s zig (%d)\n", (char *) arg, i) ;
task_sleep (1) ;
sem_up (&s2) ;
}
task_exit (0) ;
}
void TaskB (void * arg)
{
int i ;
for (i=0; i<10; i++)
{
sem_down (&s2) ;
printf ("%s zag (%d)\n", (char *) arg, i) ;
task_sleep (1) ;
sem_up (&s1) ;
}
task_exit (0) ;
}
int main (int argc, char *argv[])
{
printf ("Main INICIO\n") ;
ppos_init();
sem_create (&s1, 1) ;
sem_create (&s2, 0) ;
task_create (&a1, TaskA, "A1") ;
task_create (&a2, TaskA, " A2") ;
task_create (&b1, TaskB, " B1") ;
task_create (&b2, TaskB, " B2") ;
task_join (&a1) ;
sem_destroy (&s1) ;
sem_destroy (&s2) ;
task_join (&a2) ;
task_join (&b1) ;
task_join (&b2) ;
printf ("Main FIM\n") ;
task_exit (0) ;
exit (0) ;
}