1 // programme philosophe.c
8 // nombre de philosophes
10 // fourchette gauche du philosophe i
12 // fourchette droite du philosophe i
17 static int phiState
[N
];
22 static void test(int i
)
24 if (phiState
[i
] == faim
&& phiState
[G
] != manger
25 && phiState
[D
] != manger
) {
27 sem_post(&semPhil
[i
]);
31 static void *philosophe(void *num
)
33 int i
= *(int *)num
, nb
= 2;
40 /* essayer de manger */
45 /* attendre son tour */
46 sem_wait(&semPhil
[i
]);
47 /* à nous de manger */
48 printf("philosophe[%d] mange\n", i
);
50 printf("philosophe[%d] a fini\n", i
);
51 /* laisser manger ses voisins */
65 for (i
= 0; i
< N
; i
++)
67 for (i
= 0; i
< N
; i
++)
70 sem_init(&mutex
, 0, 1);
71 for (i
= 0; i
< N
; i
++)
72 sem_init(&semPhil
[i
], 0, 1);
73 // création des N philosophes
74 for (i
= 0; i
< N
; i
++)
75 pthread_create(&th
[i
], NULL
, philosophe
, &NumPhi
[i
]);
76 // attendre la fin des threads
77 for (i
= 0; i
< N
; i
++)
78 pthread_join(th
[i
], NULL
);
79 printf("fin des threads\n");