Professional Documents
Culture Documents
A Classic Parallel
Processing Problem
by E Dijkstra
Presented by:
Monzur Morshed
Habibur Rahman
TigerHATS
www.tigerhats.org
TigerHATS - Information is power
Philosophers eat/think
Eating needs 2 forks
Pick one fork at a time
How to prevent
deadlock
3
Definition
Extension to mutual exclusion
Arbitrary topology
Each philosopher (process)
proceeds through the following
cycle
• Thinking may
remain indefinitely
• Hungry
• Eating has to finish within finite
time
Solution properties
• Safety – no two neighbors
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 neighbour’s 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
• Some philosophers think for such a short time and contrive
to put their forks down in such a way that no other
philosophers have the opportunity to pick up the two forks
they require to eat
• Thread waits indefinitely
Livelock
• A policy that makes them all do something endlessly
without ever eating!
Deadlock Starvation but not vice versa
• Starvation can end (but doesn’t have to)
• Deadlock can’t end without external intervention
Dining Philosopher’s Problem
(Dijkstra ’71)
Deadlock - Pictorially
Starvation - Pictorially
A Different Formulation
Philosophers go the canteen for their food
They form a queue outside the canteen
cstick[5] cstick[2]
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[5] cstick[2]
cstick[4] cstick[3]
Necessary Conditions for Deadlock
Mutual exclusion
Hold and wait condition
No preemption condition
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
• Can’t just hold the fork indefinitely
• Communication only between adjacent philosophers
• No global state
• Can’t communicate with both at same time
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 code below illustrates how a semaphore is
created:
#include <semaphore.h>
sem_t mutex; /* create the semaphore and initialize it to 1 */
sem_init(&mutex, 0, 1);
sem_wait(&mutex); /* acquire the semaphore */
/*** critical section ***/
sem_post(&mutex); /* release the semaphore */
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 semaphore’s initial value (1)
Dining Philosophers(Source Code)