1 // programme philosophe.c
8 // nombre de philosophes
10 // fourchette gauche du philosophe i
12 // fourchette droite du philosophe i
19 static void *philosophe(void *num
)
21 int i
= *(int *)num
, nb
= 2;
24 sem_wait(&mutex
); // essayer de prendre les fourchettes pour manger
25 if (fourch
[G
] == libre
&& fourch
[D
] == libre
) {
30 printf("philosophe[%d] mange\n", i
);
32 printf("philosophe[%d] a fini de manger\n", i
);
33 sem_wait(&mutex
); // libérer les fourchettes
46 for (i
= 0; i
< N
; i
++)
48 for (i
= 0; i
< N
; i
++)
51 sem_init(&mutex
, 0, 1);
52 // création des N philosophes
53 for (i
= 0; i
< N
; i
++)
54 pthread_create(&th
[i
], NULL
, philosophe
, &NumPhi
[i
]);
55 // attendre la fin des threads
56 for (i
= 0; i
< N
; i
++)
57 pthread_join(th
[i
], NULL
);
58 printf("fin des threads\n");