95384c98f73eb83097a4cffcff99e520b96f8033
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
] = { penser
};
24 if (phiState
[i
] == faim
&& phiState
[G
] != manger
25 && phiState
[D
] != manger
) {
27 sem_post(&semPhil
[i
]);
31 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 */
64 int i
, NumPhi
[N
] = { 0, 1, 2, 3, 4 };
66 sem_init(&mutex
, 0, 1);
67 for (i
= 0; i
< N
; i
++) {
68 sem_init(&semPhil
[i
], 0, 1);
70 // création des N philosophes
71 for (i
= 0; i
< N
; i
++)
72 pthread_create(&th
[i
], NULL
, philosophe
, &NumPhi
[i
]);
73 // attendre la fin des threads
74 for (i
= 0; i
< N
; i
++)
75 pthread_join(th
[i
], NULL
);
76 printf("fin des threads\n");