Le problème des Philosophes


Le problème des Philosophes

Philosophe i
penser();
manger();

5 philosophes sont réunis autour d'une table pour manger des spaghetti. Chaque philosophe doit utiliser 2 fourchettes pour manger.

Problème : modéliser le comportement de chaque philosophe pour éviter les privations et les blocages.

Solution (fausse)

Philosophe i
penser();
P(fourchette i);
P(fourchette (i+1) mod 5);
manger();
V(fourchette i);
V(fourchette (i+1) mod 5);

Si tous les philosophes prennent en même temps leur fourchette i, il y a interblocage.

Solution (juste)

Philosophe i
penser();
prendre_fourchette(i);
manger();
poser_fourchette(i);
prendre_fourchette(i)
P(mutex);
état[i] = FAIM;
test(i);
V(mutex);
P(s[i]);
poser_fourchette(i)
P(mutex);
état[i] = PENSE;
test(GAUCHE);
test(DROITE);
V(mutex);

test(i)
si (état[i] == FAIM && état[GAUCHE] != MANGE && état[DROITE] != MANGE) alors
. état[i] = MANGE;
. V(s[i]);


Retour au sommaire.
Free Web Hosting