Lecture 15

Review/Preview

  recall the readers writers problem and our first solution
  it gives readers preference and is VERY difficult to modify to be fair

  we are now going to solve the problem in a different way
  the method we will use has two key attributes:
     (1) it can be used to solve ANY synchronization problem
     (2) it leads to solutions that can readily be modified to
         change the scheduling strategy


Readers/Writers Revisited

  let's start by developing a predicate that exactly characterizes
  the good and bad states

  let nr = the number of active readers (initially zero)
      nw = the number of active writers (initially zero)

  BAD state (to be avoided):
     (nr > 0 and nw > 0) or (nw > 1)

  GOOD states == not BAD states == RW: (nr = 0 or nw = 0) and nw <= 1


Coarse-Grained Solution

   we want RW to be a global invariant!  we can do so as follows:

     reader:  << await (nw == 0) nr++; >>
              "read"
              << nr--; >>

     writer:  << await (nr == 0 and nw == 0) nw++; >>
              "write"
              << nw--; >>

   that's all there is to it!  (using await statements at least)


Fine-Grained Solution

   how can we turn the above into a solution that just uses semaphores?
   need CS protection and to implement the delay in the await statements
   we actually know how to do this using split binary semaphores

   outline of basic idea of "passing the baton"

        start with baton on table
        each process:

          (1) waits to be able to pick up the baton

          (2) if (not OK to proceed) {
                 increment a counter to record that you have to wait
                 put the baton back on the table
                 take a seat and wait (DELAY)
              }

          (3) once your are awakened (or if it was OK to proceed):
              change nr or nw
              give the baton to somebody else OR put it on the table (SIGNAL)


   at this point I get my class to act out the actual algorithm
   I use a marker pen as the baton and get four volunteers:  two readers
      and two writers
   we keep track of the counters on the board, use a table in front
      for the baton, and use seats in front for the waiting "processes"
   the students like the break from normal routine and the "visualization"
      of the protocol helps them to remember it


   actual solution -- transparencies for Figures 4.12 and 4.13


Scheduling Problems and Policies

   modifying the readers/writers solution
      describe basic idea; see text for details

   shortest job next allocation -- Section 4.5.2

      request(time, id)
         if (free)
           take resource
         else
           delay by time

      release(id)
         if (some process delayed)
           awaken first one (min value of time) and give it the resource
         else
           make resource free

       transparency for Figure 4.14

       the solution uses PRIVATE SEMAPHORES
       how could we change the solution so that requests are served in FIFO order?