Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possibility of starvation of Dining Philosophers

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?

like image 834
Eng.Fouad Avatar asked Nov 25 '11 20:11

Eng.Fouad


People also ask

What is starvation in dining philosophers problem?

The problem is how to design a discipline of behavior (a concurrent algorithm) such that no philosopher will starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.

What is the main problem that you can observed in the dining philosophers problem?

A hungry philosopher may only eat if there are both chopsticks available. Otherwise a philosopher puts down their chopstick and begin thinking again. The dining philosopher is a classic synchronization problem as it demonstrates a large class of concurrency control problems.

How deadlock is possible with the dining philosophers problem?

Deadlock could occur if every philosopher holds a left chopstick and waits perpetually for a right chopstick (or vice versa). Originally used as a means of illustrating the problem of deadlock, this system reaches deadlock when there is a 'cycle of unwarranted requests'.

What are the possible solutions for dining philosopher's problem?

Wait and Signal operations will be used for the solution of the Dining Philosophers Problem, for picking a chopstick wait operation can be executed while for releasing a chopstick signal semaphore can be executed.


1 Answers

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

like image 133
Per Avatar answered Nov 05 '22 21:11

Per