1 / 26

DININING PHILOSOPHER PROBLEM

DININING PHILOSOPHER PROBLEM. 5 th March 2014 wednesday. SRAJAN GARG 11CS10045. DINING PHILOSOPHER PROBLEM. Five silent philosophers sit at a table around a bowl of  spaghetti. A fork is placed between each pair of adjacent philosophers . THE RULES.

obert
Download Presentation

DININING PHILOSOPHER 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. DININING PHILOSOPHER PROBLEM 5th March 2014 wednesday SRAJAN GARG 11CS10045

  2. DINING PHILOSOPHER PROBLEM Five silent philosophers sit at a table around a bowl of spaghetti. A fork is placed between each pair of adjacent philosophers.

  3. THE RULES • Each philosopher must alternately think and then if hungry, eat. However, a philosopher can only eat spaghetti when he has both left and right forks. • Each fork can be held by only one philosopher and so a philosopher can use the fork only if it's not being used by another philosopher. After he finishes eating, he needs to put down both forks so they become available to others. • A philosopher can grab the fork on his right or the one on his left as they become available, but can't start eating before getting both of them. • Eating is not limited by the amount of spaghetti left: assume an infinite supply.

  4. THE PROBLEM The problem is how to design a discipline of behavior such that each philosopher won't starve; i.e., can forever continue to alternate between eating and thinking assuming that any philosopher cannot know when others may want to eat or think.

  5. SOLUTION 1

  6. SOLUTION 1 • Philosopher thinks until he is hungry. • If left fork is available, pick it up. • If right fork is available, pick it up. • Eat for a certain period of time. • Putthe left fork down. • Put the right fork down. • Repeat form the beginning

  7. SOLUTION 1 #DEFINE N 5; Void Philospher(inti) //i = 1,2,3,4,5 { while(TRUE) { think(); take_fork(i); //pick up left fork take_fork((i+1)%N); //pick up right fork eat(); //yum yum, spaghetti put_fork(i); //put down left fork put_fork((i+1)%N); //put down right fork } } However, this is not a correct solution. Why????

  8. Suppose i =1; Philosopher1 thinks. When he is hungry he notices that fork1 and fork2 are available. He picks up fork1. • Now, Just before Philosopher picks up fork2, there is a context switch.Philosopher2 comes into action. • Philosopher2 thinks and notices fork2 and fork3 are available. He picks up fork2 and fork3. • Now, again a context switch occurs and Philosopher1 comes back into action. • Philosopher starts executing from the same point he left and he also picks up fork2. • So, Philosopher1 and Philosopher2 are both using a resource (fork2) mutual to each other. • Hence, this solution does not guarantees MUTUAL EXCLUSION. • What to do in order to assure MUTUAL EXCLUSION???

  9. SOLUTION 2

  10. SOLUTION 2 Implement each FORK as a SEMAPHORE

  11. SOLUTION 2 • Since There are five Forks, Implement forks as an array of FIVE Semaphores. • Initialize each fork to 1. • 1 indicates fork is available. • 0 indicates fork is in use/unavailable. 1 1 1 1 1 Semaphore fork[5]; for i 1 to 5 fork[i] = 1;

  12. SOLUTION 2 do { think(); //philosopher thinks wait(fork[i]); //wait if left fork is not available, else pick up fork & proceed wait(fork[(i+1)%5]);//wait if right fork is not available, else pick up fork & proceed eat(); //yum-yum, sphagetti signal(fork[i]); //putdown left fork & wake up waiting philosopher signal(fork[(i+1)%5]);//putdown right fork & wake up waiting philosopher } while(TRUE);

  13. SOLUTION2 • Wait(fork[i]) : decrements value of fork[i] to 0,picks the fork and proceeds, indicating ith fork is now not available. If another Philosopher tries to attain the fork, he is blocked. He then waits until the fork is available • Signal(fork[i]) : increments value of fork[i] to 1.Put down the fork. Sends a wakeup call to waiting Philosopher that the fork is available and he can use it now. • Since wait() and signal() are ATOMIC system calls, They cannot be interrupted by Context switches.

  14. SOLUTION 2 • Suppose philosopher1 has acquired fork1 and fork2 and is eating. • If there is a context switch and philosopher2 tries to acquire fork2, he will be blocked on fork[2]. • Philosopher2 wakes up when Philosopher1 executes signal(fork[(i+1)%5]; where ,i = 1 • This solution ensures MUTUAL EXCLUSION, as no two philosophers are ever allowed to use a mutual resource. • But, This solution has a Logical Error. What is it???

  15. SOLUTION 2 - error • Philosopher1 picks up left fork, there is a context switch. • Philosopher2 picks up left fork, again there is a context switch. • Similarly, Philosopher3,4,5 also pick up their left fork. • Thus all the forks are picked up simultaneously. The semaphore array of fork would look like : 0 0 0 0 0 • All the philosophers are now waiting on their right fork which is being held by their adjacent philosopher.

  16. SOLUTION 2 - error • According to this solution, a philosopher can release a fork only after eating. • And, a philosopher can eat only after acquiring both the forks. • Since each philosopher has one fork and cannot eat, they would never release the fork. • Thus the philosophers will be waiting on other fork forever and ever. • This is what leads to • Therefore this solution is also not 100% correct. DEADLOCK

  17. SOLUTION 2 – How to remove deadlock • After taking left fork, philosopher checks to see if right fork is available. • If YES, pick right fork and eat. • If NO, put down the left fork back. • Oh Cool, deadlock is removed. Is the solution now perfectly correct..??? • The answer is NO!! • what if all the philosophers pick up the left fork simultaneously!!??

  18. SOLUTION 2 • Even if the deadlock is removed by above modification, a problem still occurs if all philosophers pick up a fork simultaneously. • After picking up a fork, each philosopher will check for his right fork, which is not available as each fork was picked up simultaneously. • So, each philosopher will put down the fork back. • As a result, no philosopher would never execute eat() and will be hungry forever. • This leads to !!! STARVATION

  19. SOLUTION 3

  20. SOLUTION 3 Instead of semaphore array use a single semaphore ‘mutex’, that would ensure MUTUAL EXCLUSION.

  21. SOLUTION 3 #DEFINE N 5; Void Philospher(inti) //i = 1,2,3,4,5 { while(TRUE) { think(); wait(mutex); //wait on mutex if a philosopher is eating take_fork(i); //pick up left fork take_fork((i+1)%N); //pick up right fork eat(); //yum yum, spaghetti put_fork(i); //put down left fork put_fork((i+1)%N); //put down right fork signal(mutex); //wake up waiting philosopher } }

  22. SOLUTION 3 • This solution does not leads to or • This solution ensuresMutual Exclusion. • But, the major drawback of this solution is that this solution gives POOR RESOURCE UTILIZATION. DEADLOCK STARVATION

  23. SOLUTION 3 • philosopher1 once executes wait(mutex), picks up fork1 & fork2 and starts eating • At any point of time if there is a context switch, any of the other four philosophers has to wait on ‘mutex’ until philosopher1 finishes eating and executes signal(mutex). • Note that philosopher 3 need fork3 and fork4 which are available but he cannot eat until philosopher1 finishes. similarly philosopher 4 also has to wait even when his both forks are available. • Only one philosopher eat at a time with two forks even if other philosophers requiring different forks sits hungry. • POOR RESOURCE UTILIZATION

  24. FINAL SOLUTION • we need a solution that utilizes resources properly and does not leads to deadlocks or starvation.

  25. FINAL SOLUTION - Introduction • Use two different arrays. • int array – STATE[5]; • 0 -> Thinking :: Wants to acquire both forks. • 1 -> Hungry :: Has both the forks and after eating will put them down. • 2 -> Eating :: Is eating • Semaphore array – S[5]; • initialized to Zero Explanation of final solution covered in next lecture 

  26. Thank You

More Related