13

I need to check my algorithm of solving the dining philosopher problem if it guarantees that all of the following are satisfied or not:

  • No possibility of deadlock.
  • No possibility of starvation.

I am using the semaphore on the chopsticks to solve the problem.

Here is my code (the algorithm):

while(true)
{
    // He is Hungry
    pickup_chopsticks(i);

    // He is Eating...
    drop_chopsticks(i);

    // He is thinking
}

// ...

void pickup_chopsticks(int i)
{
    if(i % 2 == 0) /* Even number: Left, then right */
    {
        semaphore_wait(chopstick[(i+1) % NUM_PHILOSOPHERS]);
        semaphore_wait(chopstick[i]);
    }
    else /* Odd number: Right, then left */
    {
        semaphore_wait(chopstick[i]);
        semaphore_wait(chopstick[(i+1) % NUM_PHILOSOPHERS]);
    }
}

void drop_chopsticks(int i)
{
    semaphore_signal(chopstick[i]);
    semaphore_signal(chopstick[(i+1) % NUM_PHILOSOPHERS]);
}

I am sure there is no possibility of deadlock here, but is it possible to have a starvation problem here? If yes, how can I solve it?

Charles
  • 50,943
  • 13
  • 104
  • 142
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • BTW, I took this algorithm from this website: http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/ – Eng.Fouad Nov 25 '11 at 21:14
  • I don't think it causes to any problem, but proving this is hard, but there are some other proved solution like Dijkstra's solution which uses resource hierarchy and is safe and proved before. – Saeed Amiri Nov 25 '11 at 21:26
  • 1
    You're using fair semaphores, right? Otherwise, the answer is trivially yes. – Per Nov 25 '11 at 21:39

2 Answers2

17

Definitions. A philosopher is enabled iff he is not waiting for an unavailable semaphore. An execution is an infinite sequence of steps taken by enabled philosophers. An execution is strongly fair iff every philosopher enabled infinitely often takes infinitely many steps. A dining philosophers solution is starvation-free iff, in every strongly fair execution, every philosopher dines infinitely often.

Theorem. Every loop-free deadlock-free dining philosophers solution in which non-dining philosophers do not hold semaphores is starvation-free.

Proof. Assume for the sake of obtaining a contradiction that there exists a strongly fair execution in which some philosopher, call him Phil, dines only finitely often. We show that this execution is in fact deadlocked.

Since pickup_chopsticks and drop_chopsticks have no loops, Phil takes only finitely many steps. The last step is a semaphore_wait, say on chopstick i. Because the execution is strongly fair, chopstick i is necessarily continuously unavailable from some finite time onward. Let Quentin be the last holder of chopstick i. If Quentin took infinitely many steps, then he would semaphore_signal chopstick i, so Quentin takes finitely many steps as well. Quentin, in turn, is waiting on a chopstick j, which, by the same argument, is continuously unavailable until the end of time and held by (say) Robert. By following the chain of semaphore_waits among finitely many philosophers, we necessarily arrive at a cycle, which is a deadlock.

QED

Per
  • 2,594
  • 12
  • 18
  • Nice. I had to think about your claim that "chopstick i is necessarily continuously unavailable from some finite time onward", but it works out: call that time t, then either chopstick i is continuously unavailable from t onwards (which satisfies your claim), or it's available a finite number of times from t onwards (in which case we can choose some t' > t to replace t and make your claim true), or it is available an infinite number of times from t onwards (which can't happen because it would contradict the combination of "execution is strongly fair" and "Phil stopped eating"). – j_random_hacker Dec 04 '11 at 04:53
5

I haven't used this in many years but there is a tool you can use to verify your algorithm. You will have to write you algorithm in Promela.

http://spinroot.com/spin/whatispin.html

http://en.wikipedia.org/wiki/Promela

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77