1 / 11

CS252: Systems Programming

CS252: Systems Programming. Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Dinning Philosophers, and Misc. Dining Philosophers Problem. N Philosophers are in a round table. There is a fork in each side of a plate that they will use to it spaghetti.

nika
Download Presentation

CS252: Systems Programming

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. CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Dinning Philosophers, and Misc

  2. Dining Philosophers Problem • N Philosophers are in a round table. • There is a fork in each side of a plate that they will use to it spaghetti. • They need two forks to eat the spaghetti. • Chopsticks may be a better example  • They will have to share the forks with their neighbors.

  3. Dining Philosophers Problem Philosopher Spaghetti Fork

  4. Dining Philosophers Problem • Problem: • They may all decide to grab the fork in their right at the same time and they will not be able to proceed. This is a deadlock

  5. Dining Philosophers Problem Philosopher holds fork Philosopher waits for fork

  6. Dining Philosophers Problem (Unfixed Version) constint NPHILOSOPHERS=10; mutex_tfork_mutex[NPHILOSOPHERS]; pthread_tthreadid[NPHILOSOPHERS]; void eat_spaghetti_thread(inti) { while (i_am_hungry[i]) { mutex_lock(&fork_mutex[i]); mutex_lock(&fork_mutex[(i+1)%NPHILOSOPHERS); // Eat spaghetti chomp_chomp(i); mutex_unlock(&fork_mutex[i]); mutex_unlock(&fork_mutex[(i+1)%NPHILOSOPHERS); } }

  7. Dining Philosophers Problem (Unfixed Version) main() { for (inti = 0; i < NPHILOSOPHERS; i++) { mutex_init(&_fork_mutex[i]); } for (inti = 0; i < NPHILOSOPHERS; i++) { pthread_create(&threadid[i],eat_spaghetti_thread, i, NULL); } // Wait until they are done eating for (inti = 0; i < NPHILOSOPHERS; i++) { pthread_join(&threadid[i]); } }

  8. Dining Philosophers Problem (Fixed Version) Acquire the fork mutexin a particular order to prevent any deadlock.

  9. Dining Philosophers Problem (Fixed Version) void eat_spaghetti_thread(intphilosopherNum) { while (i_am_hungry[philosopherNum]) { inti = philosopherNum; int j = (philosopherNum+1)% NPHILOSOPHERS; if (i > j) { /*Swap i and j */ inttmp=i; i=j; j=tmp; } // Lock lower order mutex first mutex_lock(&fork_mutex[i]); mutex_lock(&fork_mutex[j]); // Eat spaghetti chomp_chomp(philosopherNum); mutex_unlock(&fork_mutex[i]); mutex_unlock(&fork_mutex[j]); } }

  10. Other Solutions to Dinning Philosophers • The global lock ordering solution avoids deadlock • Limited parallelism • Ensures that only N-1 philosophers can try to pick up forks • Local reordering: an odd-numbered philosopher picks up left first; even-numbered picks up right first • Better parallelism • Waiter: each philosopher needs to ask a waiter before picking up the forks (central coordination) • Random waiting: picks up left fork, and if right fork unavailable, put left fork down, wait a random time, try again • Purely local solution. • Unlucky philosopher may starve for a long time

  11. User and System Time in MT programs • In a multi processor machine • User time + system time < N* Real time • Where N is the number of processors • If your program is using the N processors at 100% then • User time + System time = N* Real time. • or • (User Time+ System time)/Real Time = N

More Related