Docsity

The Dining Philosophers Problems - Lecture Notes | EECS 678, Lab Reports of Operating Systems

Partial preview of the text

Download The Dining Philosophers Problems - Lecture Notes | EECS 678 and more Lab Reports Operating Systems in PDF only on Docsity!

PThreads Lab 3: The Dining

Philosophers Problem

Michael Jantz

Prasad Kulkarni

Introduction

โ— This lab builds off the previous two labs.

  • (^) It assumes you have an understanding of what PThreads are and how to use them.
  • (^) It is recommended to work through the previous two labs on PThreads if you have not done so already.

โ— Grab the starter code from the website:

  • (^) http://people.eecs.ku.edu/~mjantz/678labs/lab08/lab08.tar.gz

โ— Untar it and make it:

  • (^) tar xvzf lab08.tar.gz
  • (^) cd lab08/; make

Dining Philosophers (cont.)

A Classic Synchronization Problem

โ— The DPP is actually a classic synchronization problem.

  • (^) It was first proposed by Dijkstra (1965) as a problem of synchronizing the access of five computers competing for five shared tape drives.
  • (^) It was later retold as it is told here by Hoare. โ—

The DPP is not , practically, all that important.

  • (^) There are not a large class of problems that map directly to the DPP.
  • (^) However, it is an excellent example of a large class of concurrency- control problems.
  • (^) It is a simple representation of the need to allocate several resources among several processes in a deadlock and starvation free manner.

Deadlock

โ— (^) Why would we ever reach a state where none of the philosophers make progress? โ— (^) Consider the following scenario:

  • (^) Each philosopher acquires the chopstick to her left, but is interleaved before acquiring the chopstick to her right.
  • (^) After all philosophers have ran and acquired one chopstick, each philosopher is left holding the chopstick to her left and waiting for chopstick to her right to be released.
  • (^) However, the chopstick to the right of any philosopher will never be released because this chopstick is held by her neighbor philosopher (as it is her neighbor philosopher's left chopstick), who is herself waiting for the chopstick to her right to be released! โ— (^) This scenario is a classic example of deadlock. Deadlock occurs when two or more competing actions are waiting for the other to finish, and thus neither ever does.

Running the Starter Code

โ— Running the starter code should indeed produce some sort of

deadlock:

-bash-3.2$ ./dining_philosophers p0=2/10 p1=2/10 p2=2/11 p3=3/4 p4=1/ p0=0/10 p1=0/10 p2=0/11 p3=0/4 p4=0/ Reached deadlock

โ— Interpret this as follows:

  • (^) In the first five second interval in the driver program, p0 through p4 (the philosophers) each ate between 1 and 3 times (p3 ate the most with three times).
  • (^) In total, each philosopher had eaten between 1 and 11 times after the first five second interval (p2 had eaten the most with 11 times). Some eating is done before the first five second interval starts counting.
  • (^) In the next five second interval, none of the philosophers ate at all. It is determined we have reached deadlock, and the program exits.

An Asymmetric Solution

โ— The first solution is so simple it may seem inadequate at first,

but it does actually prevent deadlock.

โ— The solution is this: simply impose the condition that every

other philosopher picks up their chopsticks in a different

order.

โ— That is, (given that the philosophers are identified using

unique increasing integers) even numbered philosophers pick

up the chopstick on their left first, and odd numbered

philosophers pick up the chopstick on their right first.

โ— Implement this solution in dining_philosophers.c using me->id

to identify the even and odd philosophers in the philosopher

thread code.

Verifying the Asymmetric Solution

 - P0 P 
  • P4 P - P - C - C - C - C
    • C

The Waiter Solution

โ— There is another simple solution that we can implement

quickly that does not have this unfairness problem.

โ— Consider the following:

  • (^) The philosophers clearly can't take care of themselves, so we introduce a waiter at the table.
  • (^) In order to start eating, each philosopher must first ask the waiter if the chopsticks at her sides are available. If they are not both available, the philosopher waits until they are. Otherwise, the philosopher is granted the chopsticks, and eats.
  • (^) When the philosopher is finished eating, she must tell the waiter to signal any philosophers that may have been waiting for her chopsticks.

Implementing the Waiter Solution

โ— (^) Implement the waiters solution in dining_philosophers.c โ— (^) As a hint, I've initialized all the synchronization variables you should need. โ— (^) There is a mutex named waiter which you should use to ensure mutual exclusion when โ€œaskingโ€ the waiter and acquiring chopsticks. โ— (^) There are also some new variables for each philosopher that can be used to implement the functionality of a waiter. These are accessible from within the philosopher code using the me pointer.

  • (^) can_grab_left and can_grab_right are state variables that are updated when a philosopher receives a chopstick from or returns a chopstick to the waiter.
  • (^) can_eat is a condition variable each philosopher should wait on when it is found that she cannot eat (because she cannot acquire both chopsticks). โ— (^) IMPORTANT: You do not need mutexes for each chopstick to implement this solution. You should remove these mutexes.

Limitations of the Solution

โ— Before you go, it is important to understand some of the

limitations to the solution you've implemented.

โ— The solution does in fact prevent deadlock. At least one of

the philosophers will always make progress as the program

continues to run.

โ— However, the solution does not inherently ensure that all of

the philosophers make progress. The fact that the observed

output shows that none of the philosophers are starved is, in

part, due to the scheduling policy in Linux.

Starvation

โ— (^) To see this, imagine the following scenario:

  • (^) Both P0 and P3 are allowed to eat first. P1, P2, and P4 attempt to eat, but are forced to wait. P0 and P3 finish at roughly the same time, but the scheduler chooses P1 and P to run when the conditions are signaled. Now, P0 and P3 both attempt to eat while P and P2 are eating and are told to wait. When P1 and P2 complete, the scheduler, which is unsympathetic to the needs of P4, chooses P0 and P3 to run, and the cycle continues.
  • (^) P4 starves because conditions arise that allow the scheduler to never choose it. โ— (^) We do not see this behavior in our output because the Linux scheduler takes extra precautions to ensure that processes that can run don't starve. โ— (^) We could implement a starvation free solution that did not require us to make assumptions about the system's scheduler by keeping track of which processes are making progress in user land.