-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieces.c
63 lines (48 loc) · 1.22 KB
/
pieces.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
static sem_t sem1, sem2;
int the_end;
int trait1 = 1;
int trait2 = 5;
int nb_p_table = 0;
void *thread1_process (void * arg)
{
while (!the_end) {
sem_wait (&sem2);
printf ("Robot 1 traite une piece ! \n");
sleep (trait1);
printf ("Robot 1 depose 1 piece sur la table! nb piece sur table %d\n",++nb_p_table);
sem_post (&sem1);
}
pthread_exit (0);
}
void *thread2_process (void * arg)
{
while (!the_end) {
sem_wait (&sem1);
sleep (trait2);
printf ("Robot 2 a pris une piece nb piece sur table %d !\n",--nb_p_table);
sem_post (&sem2);
printf ("Robot 2 fini 1 piece \n");
}
pthread_exit (0);
}
main (int ac, char **av)
{
pthread_t th1, th2;
void *ret;
sem_init (&sem1, 0, 0);
sem_init (&sem2, 0, 4);
if (pthread_create (&th1, NULL, thread1_process, NULL) < 0) {
fprintf (stderr, "pthread_create error for thread 1\n");
exit (1);
}
if (pthread_create (&th2, NULL, thread2_process, NULL) < 0) {
fprintf (stderr, "pthread_create error for thread 2\n");
exit (1);
}
(void)pthread_join (th1, &ret);
(void)pthread_join (th2, &ret);
}