# The readers writers problem has several variations

• Notes
• 39

This preview shows page 20 - 24 out of 39 pages.

Subscribe to view the full document.

Synchronization 21 41 Readers and Writers Problem (cont’d) ! A reader and writer processes: do { wait(wrt); ... // writing is performed ... signal(wrt); } while(TRUE); do { wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); ... // reading is performed ... wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex); } while(TRUE); 42 Dining Philosophers Problem ! Five philosophers spend their lives thinking and eating. ! The table is laid with five single chopsticks. ! When a philosopher gets hungry, she tries to pick up the two chopsticks that are closet to her. " Only one chopstick can be picked up at a time. " Obviously, she can not pick up a chopstick that is already in the hand of a neighbor. ! When a philosopher is finished eating, she puts down both of her chopsticks and starts thinking again.
Synchronization 22 43 Dining Philosophers Problem (cont’d) ! This problem is considered a classic synchronization problem. " It represents the need to allocate several resources among several processes in a deadlock-free and starvation-free manner. ! A plausible solution: " To represent each chopstick with a semaphore. semaphore chopstick[5]; \$ All initialized to 1. " A philosopher tries to grab a chopstick by executing a wait() operation on that semaphore. " The philosopher releases his/her chopsticks by executing the signal() . 44 Dining Philosophers Problem (cont’d) ! The structure of philosopher i : do { wait(chopstick[i]); wait(chopstick[(i+1) % 5]); ... // eat ... signal(chopstick[i]); signal(chopstick[(i+1)%5]); ... // think ... }while (TRUE);

Subscribe to view the full document.

Synchronization 23 45 Dining Philosophers Problem (cont’d) ! What is the problem with the last solution?? " DEADLOCK!! ! Suppose that all five philosophers become hungry simultaneously and each grabs her left chopstick. " When each philosopher tries to grab her right chopstick, she will be delayed forever. ! Possible remedies: " At most four philosophers to be sitting simultaneously at the table.
You've reached the end of this preview.
• Spring '12
• GwangS.Jung
• producer, monitor

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern