Search code examples
operating-systemdeadlocksemaphorecritical-sectiondining-philosopher

mixture of left-handed and right-handed philosophers, a tricky questions?


Lemma 1: we know at any table with a mixture of left-handed and right-handed philosophers, deadlock cannot occur. I very familiar with it proofs.

I ran into a following question on Interview recently.

There are 5 philosophers sitting at a round table. between each two philosophers there is a chopsticks. each philosophers need two chopsticks to eat. we have two type of philosophers: left-handed and right-handed. left-handed first take a chopsticks with left hands. right-handed first take a chopsticks with right hands. suppose among 5 philosophers there is at least 1 left-handed and at least 1 right-handed. which of them is True:

a) independent from the combination of placement philosophers at round table, there is no deadlock. (I sure True)

b) if all of the philosophers simultaneously take the first chopsticks, there is a deadlock. (I thin this is true because in implication if we have a-->b and a is false a-->b is true., and in this option there is no way to all philosopher simultaneously take the first chopsticks, so the whole statement is true.)

Any expert could help me, that I argument is True?

Edit 1: I add the proof for Lemma1: (but the question mentioned above has some differences. )

First observe that due to the pattern of resource acquisition around the table there is only one possible wait-cycle, i.e., the cycle involving all philosophers. So assume such a deadlock. Every philosopher must be waiting, by which we mean waiting for a chopstock held by another. Thus all chopsticks must be held. If our table has a mixture of left-handed and right-handed philosophers, there must be at least one pair of adjacent philosophers of opposite handedness. First assume a left-handed philosopher (“Lenny”) seated to the left of a right-handed philosopher (“Roger”). Because all chopsticks must be held, either Lenny or Roger must hold the chopstick which is between them. If Lenny holds it, that is his “right chopstick,” so he must previously have acquired the chopstick to his left. Thus Lenny holds two chopsticks, and is not waiting to acquire any (he is eating), so there is not a deadlock. If Roger holds the chopstick which is between them, that is his “left chopstick,” so he must previously have acquired the chopstock to his right, etc. If we call the previous case the “outward reaching” case, then the “inward reaching” case is a right-handed philosopher seated to the left of a left-handed philosopher. Again, if we have a deadlock, all chopsticks must be held, so one of them holds the chopstick between them. This means that the other one holds no chopsticks, since the one between them is the first one each will try to acquire (either Roger reached right and got it, or Lenny reached left and got it; the other is waiting for it before reaching in the outward direction). If there are n philosphers and n chopsticks, all chopsticks held, but one philosopher holds zero chopsticks, then n−1 philosophers hold n chopsticks. Some philospher must hold two chopsticks; that philosopher is not waiting (he is eating), so there is no deadlock.


Solution

  • Here is an example case:

    one left handed philosopher, four right handed philosophers: If all pick up one chopstick, an the left handed philosopher wins the struggle for the chopstick on his left hand side, he has one chopstick and the one on his right hand side is still free. In the next iteration either he, or his right hand neighbor will pick up the last free chopstick. Either way at least one philosopher has two chopsticks to eat. --> no deadlock

    The assumption is, that no philosopher tries to pick up, the "outward" chopstick unless, he has already acquired the "inward" one.

    Now let's dissect question b): If all philosophers manage to acquire a chopstick at the same point in time, all philosophers must be either left handed or all must be right handed. Because then there is no struggle for a chopstick and every philosopher holds one chopstick. But this violates your precondition: at least one left and one right handed philosopher.

    Assuming all but one are right handed, two philosophers will struggle for one chopstick. One will lose this struggle and will not be able to pick up a chopstick. Hence, the condition "all pick up a chopstick simultaneously" can not be fulfilled. As you correctly state, the precondition for the implication is false, hence, the implication evaluates to true.

    So to answer your question: Yes, your reasoning is correct.