Dining Philosophers: A Classic Parallel Processing Problem by E Dijkstra
Dining Philosophers: A Classic Parallel Processing Problem by E Dijkstra
by E Dijkstra
TigerHATS
www.tigerhats.org
Definition
Extension to mutual exclusion Arbitrary topology Each philosopher (process) proceeds through the following cycle
Thinking may
Solution properties
eat at the same time Liveness each hungry philosopher eventually eats
Problem Statement
Five philosophers eat then think forever They never sleep nor relieve themselves! They do not behave altruistically They eat at a communal table It has a single bowl of tangled spaghetti Five plates each with a single fork to the left of their
plate To eat a philosopher must have two forks, their own and that of their neighbours to the right If a philosopher is unable to eat they resume thinking
Ramifications
Deadlock
All philosophers decide to eat at same time They all pick up one fork None of them can eat hence the system comes to a halt Circular waiting for resources
Starvation
Livelock
Starvation can end (but doesnt have to) Deadlock cant end without external intervention
Deadlock - Pictorially
Starvation - Pictorially
A Different Formulation
Philosophers go the canteen for their food They form a queue outside the canteen A Chef brings chickens to the canteen in batches that are less than the number of philosophers Philosophers connected to Canteen by means of Any channels
Behaviour
Philosophers go hungry They can get into the canteen even when there
are no chickens Need to ensure that philosophers queue until chickens available
Only the Canteen process needs to be modified Simply needs a precondition on the ALT
Philosopher i while (true) { //think for a while, getting hungry chopstick[i].P(); chopstick[(i+1) % 5].P(); //eat now; (critical section) chopstick[i].V(); chopstick[(i+1) % 5].V(); }
What is deadlock?
When some system processes are blocked on resource requests that can never be satisfied unless drastic actions are taken, the processes are deadlocked. Three approaches to deadlock Prevent deadlock by careful system analysis Detect deadlock when it happens, and take
corrective action Ignore the problem and hope for the best (this is the Unix and Windows model)
Causes of Deadlock
Strict deadlock is caused by cyclic resource requests Effective deadlock is caused by resource depletion (for example, not enough memory to run a large process thread)
Owned By Res 1 Wait For Thread A Wait For Res 2 Thread B Owned By
cstick[4]
cstick[3]
Avoiding Deadlock(1)
philosopher 1 philosopher 2 philosopher 3 philosopher 4 philosopher 5 cstick[1].P(); cstick[2].P(); cstick[3].P(); cstick[4].P(); cstick[1].P(); cstick[2].P(); cstick[3].P(); cstick[4].P(); cstick[5].P(); cstick[5].P();
cstick[4]
cstick[3]
Critical Regions(1)
region No assumptions made about speeds or numbers of CPUs No process running outside its critical region may block another process No process must wait forever to enter its critical region
Critical Regions(2)
Simple: waiting state Enter waiting state when neighbors eating When neighbors done, get forks Neighbors cant enter waiting state if neighbor
waiting
Problem: Doesnt prevent starvation Requires checking both neighbors at once Race condition
Waiter solution
Since the philosophers don't speak with each other One solution is to have them speak to a waiter which keeps an
overview of the table, he can then decide who gets forks and when.
Resource hierarchy consists of ordering the forks by number. It always be requested by order and released in the reverse order.
The forks as either being clean or dirty and based on this
categorization the philosophers requests the forks from one another. It request equals communication and so the restraint inherent to the problem has been violated - So this is really no solution at all.
Problem with previous solutions Not truly distributed: Requires some sort of central
Additional properties: Deadlock free: Eventually someone new gets to eat Lockout free: Eventually every hungry philosopher gets to eat Adversary: One philosopher may try to starve another Communication only between adjacent philosophers
No global state Cant communicate with both at same time Cant just hold the fork indefinitely
Semaphore solution
A semaphore is a variable or abstract data type Semaphores are used for synchronization and mutual exclusion by indicating the availability and number of resources. The pthread package provides two types of semaphores named and unnamed. For this project, we use unnamed or Binary semaphores. Binary semaphore integer value can range only between 0 and 1; can be simpler to implement
Semaphore solution
The sem_init() creates a semaphore and initialize it. This function is passed three parameters: 1. A pointer to the semaphore 2. A flag indicating the level of sharing ( flag o indicating that this semaphore can only be shared by threads belonging to the same process that created the semaphore. A nonzero value would allow other processes to access the semaphore as well. 3. The semaphores initial value (1)
Thank You