Document Sample
Concurrency Powered By Docstoc
					     Day 13
- Programming
language support
   Programming language construct that
    guarantees mutual exclusion
   A software module that consists of procedures, an
    initialization routine, and local data.
       Local data is accessible only to the procedures of the
       A process can enter a monitor by invoking one of its
       Only one process may be executing in the monitor at any
        time. Other processes that would like to invoke a monitor
        procedure will be blocked.
   Monitor Student {
        private float age;

        public synchronized void setAge(float age){ ..}

        public synchronized float getAge(){..}
Mutual exclusion
   Mutual exclusion is guaranteed:
       only one process allowed in the monitor
       place the shared data structure in a monitor.
Synchronization using
   condition variables - contained within the monitor
    and accessible only within the monitor
       A queue is associated with each condition variable
   cwait(c) – suspend execution of process on
    condition c – Monitor is now available for use by
    another process.
   csignal(c) – Resume execution of some process
    blocked on c. If there are none, then do nothing.
Readers/Writers problem
   Many readers
   Many writers
   Any number of readers can be allowed to
    read simultaneously
   While a writer is writing, no others can write.
    No readers should read either.
Monitors – Readers/Writers
Monitor ReaderWriter;
/* The following can only be accessed by
   monitor functions. */
int readers = 0;
boolean writelock = false;
Condition canWrite;
Condition canRead;
Functions: beginRead(), endRead(),
   beginWrite(), endWrite()
Reader(){        Writer(){

 while(true){              while(true){
  beginRead();              beginWrite();
  Read();                   Write();
  endRead();                 endWrite();
  }                         }
}                      }
Monitor procedures for the
Reader(Hoare’s model)
void beginRead(){       void endRead(){
  if(writeLock ||         readers--;
    queue(canWrite)){     if(readers == 0){
     cwait(canRead);           csignal(canWrite);
   }                      }
   readers++;           }
Monitor procedures for the
Writers(Hoare’s model)
void beginWrite(){         void endWrite(){
  if(readers > 0 ||          writeLock = false;
    writeLock){              if(queue(canRead)
        cwait(canWrite);          csignal(canRead);
    }                        else
    writeLock = true;         if (queue(canWrite))
}                                 csignal(canWrite);
Hoare’s model
   Hoare’s model requires that a process
       signals at the end of the procedure and leaves the monitor.
       If signal() is not the last operation, then the process making
        the signal must block and instead of being placed on any of
        the conditions queues or the entry queue, will be placed in
        the urgent queue.
       The signal will wake the first process in the queue for that
       If no process is waiting, the signal is lost.
Disadvantages of Hoare’s model
   If the process that signaled is not done with
    the monitor, then there is an un-necessary
    process switch.
   Process scheduling must be very strictly
 Reader 1:                       Writer 1:
 void beginRead(){               void endWrite(){
   if(writeLock ||                    writeLock = false;      Writer 2:
      queue(canWrite)){             if(queue(canRead)         void beginWrite(){
      cwait(canRead);                    csignal(canRead);       if(readers > 0 ||
    }                              else                                       writeLock){
    readers++;                       if (queue(canWrite))                cwait(canWrite);
                                        csignal(canWrite);         }
                                                                writeLock = true;
 }                               }

Reader 1 is blocked on canRead because writeLock is true.
Writer 1 signals canRead.
Reader 1 is un-blocked and is ready, but is not immediately scheduled.
Writer 2 is scheduled – sets writeLock to true and is ready to write - IS
WRITING when timed out.
Reader 1 gets scheduled to run – Will increment “readers”, signals to
any waiting readers and then PROCEED TO READ.
Lampson and Redell suggested a

   cnotify() instead of csignal() - Process will
    notify the first process waiting on “c”. But, the
    process waiting need not be scheduled
    immediately and the notifying process need
    not quit or block.
   However, the code must change, such that
    any process that has to continue will check
    the condition again.
Monitor procedures for the Reader(Lampson
and Redell’s model)

void beginRead(){       void endRead(){
  while(writeLock ||      readers--;
   queue(canWrite)){      if(readers == 0){
    cwait(canRead);            cnotify(canWrite);
  }                       }
  readers++;            }
Monitor procedures for the Writers(Hoare’s

void beginWrite(){        void endWrite(){
  while(readers > 0 ||      writeLock = false;
   writeLock){              if(queue(canRead)
       cwait(canWrite);          cnotify(canRead);
   }                        else
   writeLock = true;         if (queue(canWrite))
}                                cnotify(canWrite);
   cbroadcast(c) – Wake all processes that are waiting
    on a condition.
    For example, if n processes are waiting on space in
       If “j” bytes were available and now “k” more bytes become
        The memory manager does not know which processes
        waiting can run with “k + j” bytes.
       Using cbroadcast(), it can wake all the processes that are
        waiting and the process that can run with “k + j” bytes will
How do we prevent more than one process from being
in the monitor at any given time?

   Every method in the monitor becomes a critical section and any of the
    methods we have seen earlier can be used to ensure that while a
    process is in a CS, no other process can be allowed to enter any of the
    other functions.

Shared By: