The readers writers problem has several variations

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

The readers-writers problem has several variations, all involving priorities. " The first readers-writers problem: $ No reader will be kept waiting unless a writer has already obtained permission to use the shared object. $ Or, no reader should wait for other readers to finish. $ Or readers have higher priorities. " The second readers-writers problem: $ Once a writer is ready, the writer performs its write as soon as possible. ! Here, we present a solution to the first readers-writers problem. 40 Readers and Writers Problem (cont’d) ! The reader processes share the following data structures: semaphore mutex, wrt; int readcount; " readcount is initialized to 0. $ Keep track of how many processes are currently reading the database. " mutex and wrt are initialized to 1. $ mutex is used to ensure mutual exclusion when the variable readcount is updated. $ wrt , shared with writers , functions as a mutual-exclusion semaphore for the writers.
Image of page 20

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.
Image of page 21
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);
Image of page 22

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.
Image of page 23
Image of page 24
You've reached the end of this preview.
  • Spring '12
  • GwangS.Jung
  • producer, monitor

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern