1 / 17

Lecture 15: Dining Philosophers Problem

Lecture 15: Dining Philosophers Problem. Review: Mutual Exclusion Solutions. Software solution Disabling interrupts Strict alternation Peterson’s solution Hardware solution TSL/XCHG Semaphore Conditional variables POSIX standards. Review: Pthreads APIs for condition variables.

myrtlel
Download Presentation

Lecture 15: Dining Philosophers Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Thread 1: printf(“hello “); pthread_mutex_lock(&mutex); thread1_done = 1; pthread_cond_signal(&cv); pthread_mutex_unlock(&mutex); Review: Using condition variables int thread1_done = 0; pthread_cond_t cv; pthread_mutex_t mutex; Thread 2: pthread_mutex_lock(&mutex); while (thread1_done == 0) { pthread_cond_wait(&cv, &mutex); } printf(“ world\n“); pthread_mutex_unlock(&mutex);

  5. 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); } Review: Deadlock thread a thread b

  6. In this lecture • Dining Philosophers Problem • Readers-Writers Problem

  7. Dining Philosophers • Classic Synchronization Problem • Philosopher • eat, think • 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. DEADLOCK

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

  12. Another Solution 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(left_chopstick); unlock(right_chopstick); Problem: starvation if unfavorable scheduling!

  13. 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

  14. 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(left_chopstick); unlock(right_chopstick);

  15. 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

More Related