Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15: Dining Philosophers Problem

Similar presentations


Presentation on theme: "Lecture 15: Dining Philosophers Problem"— Presentation transcript:

1 Lecture 15: Dining Philosophers Problem

2 Review: Mutual Exclusion Solutions
Software solution Disabling interrupts Strict alternation Peterson’s solution Hardware solution TSL/XCHG Semaphore Conditional variables POSIX standards

3 Review: Pthreads APIs for condition variables

4 Review: Using condition variables
int thread1_done = 0; pthread_cond_t cv; pthread_mutex_t mutex; Thread 1: printf(“hello “); pthread_mutex_lock(&mutex); thread1_done = 1; pthread_cond_signal(&cv); pthread_mutex_unlock(&mutex); Thread 2: pthread_mutex_lock(&mutex); while (thread1_done == 0) { pthread_cond_wait(&cv, &mutex); } printf(“ world\n“); pthread_mutex_unlock(&mutex);

5 Review: Deadlock thread a thread b proc1( ) { pthread_mutex_lock(&m1);
/* use object 1 */ pthread_mutex_lock(&m2); /* use objects 1 and 2 */ pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); } proc2( ) { pthread_mutex_lock(&m2); /* use object 2 */ pthread_mutex_lock(&m1); /* use objects 1 and 2 */ pthread_mutex_unlock(&m1); pthread_mutex_unlock(&m2); } In this example our threads are using two mutexes to control access to two different objects. Thread 1, executing proc1, first takes mutex 1, then, while still holding mutex 1, obtains mutex 2. Thread 2, executing proc2, first takes mutex 2, then, while still holding mutex 2, obtains mutex 1. However, things do not always work out as planned. If thread 1 obtains mutex 1 and, at about the same time, thread 2 obtains mutex 2, then if thread 1 attempts to take mutex 2 and thread 2 attempts to take mutex 1, we have a deadlock. thread a thread b

6 In this lecture Dining Philosophers Problem Readers-Writers Problem

7 Dining Philosophers Classic Synchronization Problem Philosopher
eat, think …….. Philosopher = Process Eating needs two resources (chopsticks)

8 Problem: need two chopsticks to eat

9 First Pass at a Solution
One Mutex for each chopstick Philosopher i: while (1) { Think(); lock(Left_Chopstick); lock(Right_Chopstick); Eat(); unlock(Left_Chopstick); unlock(Right_Chopstick); }

10

11

12 DEADLOCK

13 One Possible Solution Use a mutex for the whole dinner-table
Philosopher i: lock(table); Eat(); Unlock(table); Performance problem!

14 Another Solution Problem: starvation if unfavorable scheduling!
Philosopher i: Think(); unsuccessful = 1; while (unsuccessful) { lock(left_chopstick); if (try_lock(right_chopstick)) /* try_lock returns immediately if unable to grab the lock */ unsuccessful = 0; else unlock(left_chopstick); } Eat(); unlock(right_chopstick); Problem: starvation if unfavorable scheduling!

15 In Practice Starvation will probably not occur
We can ensure this by adding randomization to the system: Add a random delay before retrying Unlikely that our random delays will be in sync too many times

16 Solution with Random Delays
Philosopher i: Think(); while (unsuccessful) { wait(random()); lock(left_chopstick); if (trylock(right_chopstick)) unsuccessful = 0; else unlock(left_chopstick); } Eat(); unlock(right_chopstick);

17 Solution without random delay
Do not try to take forks one after another Don’t have each fork protected by a different mutex Try to grab both forks at the same time


Download ppt "Lecture 15: Dining Philosophers Problem"

Similar presentations


Ads by Google