110 likes | 207 Views
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.
E N D
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. • They need two forks to eat the spaghetti. • Chopsticks may be a better example • They will have to share the forks with their neighbors.
Dining Philosophers Problem Philosopher Spaghetti Fork
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
Dining Philosophers Problem Philosopher holds fork Philosopher waits for fork
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); } }
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]); } }
Dining Philosophers Problem (Fixed Version) Acquire the fork mutexin a particular order to prevent any deadlock.
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]); } }
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
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