Tuesday 13 March 2012

Monitor Implementation Using Semaphores


  •           Variables
                         semaphore mutex;  // (initially  = 1)
                         semaphore next;     // (initially  = 0)
                         int next-count = 0;
  •           Each external procedure F will be replaced by
                         wait(mutex);
                         …
                         body of F;
                         …
                         if (next-count > 0)
                         signal(next)
                         else
                         signal(mutex);
  •          Mutual exclusion within a monitor is ensured.
  • For each condition variable x, we  have:
                semaphore x-sem; // (initially  = 0)
                int x-count = 0;
  • The operation x.wait can be implemented as: 

                x-count++;
                if (next-count > 0)
                signal(next);
                else
                signal(mutex);
                wait(x-sem);
                x-count--;
  • The operation x.signal can be implemented as:

                if (x-count > 0)
                {
                next-count++;
                signal(x-sem);
                wait(next);
                next-count--;
                }

1 comment:

  1. Hi, can you provide a proper explanation for mutual exclusion withing monitor by using mutex and next semaphores.

    ReplyDelete