1 // programme philosophe.c
8 // nombre de philosophes
10 // fourchette gauche du philosophe i
12 // fourchette droite du philosophe i
15 static int fourch
[N
] = { libre
, libre
, libre
, libre
, libre
};
19 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
45 int i
, NumPhi
[N
] = { 0, 1, 2, 3, 4 };
47 sem_init(&mutex
, 0, 1);
48 // création des N philosophes
49 for (i
= 0; i
< N
; i
++)
50 pthread_create(&th
[i
], NULL
, philosophe
, &NumPhi
[i
]);
51 // attendre la fin des threads
52 for (i
= 0; i
< N
; i
++)
53 pthread_join(th
[i
], NULL
);
54 printf("fin des threads\n");