SlideShare a Scribd company logo
What Is a Monitor? - Basics
qMonitor is a highly structured programming-
 language construct. It consists of
  vPrivate variables and private procedures that
    can only be used within a monitor.
  vConstructors that initialize the monitor.
  vA number of (public) monitor procedures that
    can be invoked by users.
qNote that monitors have no public data.
qA monitor is a mini-OS with monitor procedures
 as system calls.
                                              1
Monitor: Mutual Exclusion 1/2
qNo more than one process can be executing
 within a monitor. Thus, mutual exclusion is
 guaranteed within a monitor.
qWhen a process calls a monitor procedure and
 enters the monitor successfully, it is the only
 process executing in the monitor.
qWhen a process calls a monitor procedure and
 the monitor has a process running, the caller
 will be blocked outside of the monitor.

                                                   2
Monitor: Mutual Exclusion 2/2
processes waiting        qIf there is a process
to enter monitor
                          executing in a
                          monitor, any
                          process that calls a
                          monitor procedure
                          is blocked outside of
                          the monitor.
                         qWhen the monitor
                          has no executing
                          process, one process
                          will be let in.
                                            3
Monitor: Syntax
monitor Monitor-Name              qAll variables are private.
{
                                   Why? Exercise!
   local variable declarations;
                                  qMonitor procedures are
    Procedure1(…)                  public; however, some
    { // statements };             procedures can be made
    Procedure2(…)                  private so that they can
    { // statements };
                                   only be used within a
    // other procedures
    {
                                   monitor.
      // initialization           qInitialization procedures
    }                              (i.e., constructors)
}                                  execute only once when
                                   the monitor is created.
                                                           4
Monitor: A Very Simple Example
monitor IncDec              process Increment
{                           while (1) {
   int count;                  // do something
   void Increase(void)         IncDec.Increase();
   { count++; }                cout <<
                                 IncDec.GetData();
    void Decrease(void)        // do something
    { count--; }            }

    int GetData(void)
    { return count; }

    {   count = 0; }      initialization
}                                               5
Condition Variables
qWith monitors, mutual exclusion is an easy
 task.
qWhile the process is executing within a
 monitor, a programmer may want to block
 this process and force it to wait until an
 event occurs.
qThus, each programmer-defined event is
 artificially associated with a condition
 variable.
                                             6
qA condition variable, or a condition, has a
Condition wait
qLet cv be a condition variable. The use of
 methods signal and wait on cv are
 cv.signal() and cv.wait().
qCondition wait and condition signal can only be
 used within a monitor.
qA process that executes a condition wait blocks
 immediately and is put into the waiting list of
 that condition variable.
qThis means that this process is waiting for the
 indicated event to occur.
                                               7
Condition signal
qCondition signal is used to indicate an event
 has occurred.
qIf there are processes waiting on the signaled
 condition variable, one of them will be released.
qIf there is no waiting process waiting on the
 signaled condition variable, this signal is lost as if
 it never occurs.
qConsider the released process (from the signaled
 condition) and the process that signals. There
 are two processes executing in the monitor, and
 mutual exclusion is violated!
                                                    8
Two Types of Monitors
qAfter a signal, the released process and the
 signaling process may be executing in the monitor.
qThere are two common and popular approaches to
 address this problem:
  vHoare Type (proposed by C.A.R.Hoare): The
    released process takes the monitor and the
    signaling process waits somewhere.
  vMesa Type (proposed by Lampson and Redell):
    The released process waits somewhere and the
    signaling process continues to use the monitor.

                                                 9
What do you mean by
          “waiting somewhere”?
qThe signaling process (Hoare type) or the released
 process (Mesa type) must wait somewhere.
qYou could consider there is a waiting bench in a
 monitor for these processes to wait.
qAs a result, each process that involves in a monitor
 call can be in one of the four states:
  vActive: The running one
  vEntering: Those blocked by the monitor
  vWaiting: Those waiting on a condition variable
  vInactive: Those waiting on the waiting bench
                                                  10
Monitor with Condition Variables

                     qProcesses
                      suspended due to
                      signal/wait are in
                      the Re-entry list
                      (i.e., waiting
                      bench).
                     qWhen the
                      monitor is free, a
                      process is
                      released from
                      either incoming
                      or re-entry . 11
What is the major difference?
Condition    UntilHappen;      With Hoare type, once
                               a signal arrives, the signaler
// Hoare Type                  has yielded the monitor to the
if (!event)                    released process and the
  UntilHappen.wait();          condition is not changed.
                               Thus, a if is sufficient.
// Mesa Type
while (!event)        With Mesa type, the released
  UntilHappen.wait(); process may be suspended for a
                      while before it runs. During this
                      period, other processes may be in
                      the monitor and change the
                      condition. It is better to check the
                      condition again with a while!  12
Monitor: Dining Philosophers Revisited
qInstead of picking up
 chopsticks one by one, we
 insist that a philosopher
 can eat only if he can pick
 up both simultaneously.
qCan we use a semaphore
 to protect chopsticks 0
 and 1, another for 1 and
 2, and so on? No, no, no.
qRace condition!!!!!
                                      13
Monitor Definition
monitor Control             int CanEat(int i)
{                           {
   bool used[5];              if (!Used[i] &&
   condition self[5];              !Used[(i+1)%5])
   private:                     return TRUE;
      int CanEat(int);        else
                                return FALSE;
    procedure GET(int);     }
    procedure PUT(int);

    { // initialization     Function CanEat() returns
      for (i=0;i<5;i++)     TRUE if both chops for
         used[i] = FALSE;   Philosopher i are available.
    }
                                                   14
}
Monitor: GET() and PUT()
Why a while rather than a if even with a Hoare monitor?
void GET(int i)         void PUT(int i)
{                       {
  while (!CanEat(i))      Used[i] = FALSE;
    self[i].wait();       Used[(i+1)%5]
  Used[i] = TRUE;            = FALSE;
  Used[(i+1)%5] = TRUE;   for (i=0;i<5;i++)
}                           self[i].signal();
                        }
 qIn fact, PUT() only requires to signal
  self[(i+1)%5] and self[(i+4)%5], the two
  neighbors of philosopher i.
 qDoes it really matter? Why?            15
Monitor: Producer/Consumer

                 monitor ProdCons
                 {
                   int count, in, out;
                   int Buf[SIZE];
                   condition
                      UntilFull,
                      UntilEmpty;

                     procedure PUT(int);
                     procedure GET(int *);
                     { count = 0}
bounded-buffer
                 }
                                       16
Monitor: PUT() and GET()
void PUT(int X)         void GET(int *X)
{                       {
  if (count == SIZE)      if (count == 0)
    UntilEmpty.wait();      UntilFull.wait();
  Buf[in] = X;            *X = Buf[out];
  in = (in+1)%SIZE;       out=(out+1)%SIZE;
  count++;                count--;
  if (count == 1)         if (count == SIZE-1)
    UntilFull.signal();     UntilEmpty.signal();
}                       }

  qChange if to while for Mesa type monitors.
                                                17
Dining Philosophers: Again!
qIn addition to thinking and eating, a philosopher
 has one more state, hungry, in which he is trying
 to get chops.
qWe use an array state[] to keep track the state
 of a philosopher. Thus, philosopher i can eat (i.e.,
 state[i] = EATING) only if his neighbors are
 not eating (i.e., state[(i+4)%5] and
 state[(i+1)%5] are not EATING).



                                                  18
Monitor Definition
monitor philosopher
{
  enum { THINKING,HUNGRY,
         EATING} state[5];
  condition self[5];
  private: test(int);

    procedure GET(int);
    procedure PUT(int);

    { for (i=0;i<5;i++)
        state[i] = THINKING;
    }
}                              19
The test() Procedure
                            the left and right neighbors of
        void test(int k)    philosopher k are not eating
        {
            if ((state[(k+4)%5] != EATING) &&
                (state[k] == HUNGRY) &&
                (state[(k+1)%5] != EATING)) {
philosopher
                    state[k] = EATING;
k is hungry
                    self[k].signal();
            }
        }
  qIf the left and right neighbors of philosopher k
   are not eating and philosopher k is hungry, then
   philosopher k can eat. Thus, release him!
                                                      20
The GET() and PUT() Procedures
                        I am hungry
void GET(int i)
{                             see if I can eat
   state[i] = HUNGRY;                   If I could not eat,
   test(i);                             Then block myself
   if (state[i] != EATING)
        self[i].wait();
}
    I finished eating     void PUT(int i)
                          {
   Let my neighbors          state[i] = THINKING;
   use my chops              test((i+4) % 5);
                             test((i+1) % 5);
                          }
           Which type of monitor am I using?          21
How about Deadlock?
       void test(int k)
       {
         if ((state[(k+4)%5] != EATING) &&
             (state[k] == HUNGRY) &&
             (state[(k+1)%5] != EATING)) {
                state[k] = EATING;
                self[k].signal();
         }
       }

qThis solution does not have deadlock, because
 vThe only place where eating permission is
   granted is in procedure test(), and …..
 vPhilosopher k can eat only if his neighbors are
   not eating. Thus, no two neighboring
   philosophers can eat at the same time.       22
Hoare Type vs. Mesa Type
qWhen a signal occurs, Hoare type monitor uses
 two context switches, one switching the signaling
 process out and the other switching the released
 in. However, Mesa type monitor uses one.
qProcess scheduling must be very reliable with
 Hoare type monitors to ensure once the signaling
 process is switched out the next one must be the
 released process. Why?
qWith Mesa type monitors, a condition may be
 evaluated multiple times. However, incorrect
 signals will do less harm because every process
 checks its own condition.
                                               23
Semaphore vs. Condition

       Semaphores                Condition Variables
Can be used anywhere, but     Can only be used in monitors
not in a monitor
wait() does not always        wait() always blocks its
block its caller              caller
signal() either releases a    signal() either releases a
process, or increases the     process, or the signal is lost as
semaphore counter             if it never occurs
If signal() releases a        If signal() releases a
process, the caller and the   process, either the caller or
released both continue        the released continues, but
                              not both                      24

More Related Content

What's hot

INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
System calls
System callsSystem calls
System calls
Bernard Senam
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
usmankiyani1
 
MULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULINGMULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULING
garishma bhatia
 
17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria 17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria myrajendra
 
Synchronization hardware
Synchronization hardwareSynchronization hardware
Synchronization hardware
Saeram Butt
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
Mukesh Chinta
 
Os services
Os servicesOs services
Os services
anilkumar_mca
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
Adeel Rasheed
 
Resource management
Resource managementResource management
Resource management
Dr Sandeep Kumar Poonia
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
Operating system 08 time sharing and multitasking operating system
Operating system 08 time sharing and multitasking operating systemOperating system 08 time sharing and multitasking operating system
Operating system 08 time sharing and multitasking operating system
Vaibhav Khanna
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
Mohammad Hafiz-Al-Masud
 
evolution of operating system
evolution of operating systemevolution of operating system
evolution of operating system
Amir Khan
 

What's hot (20)

Mutual exclusion and sync
Mutual exclusion and syncMutual exclusion and sync
Mutual exclusion and sync
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
System calls
System callsSystem calls
System calls
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
 
MULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULINGMULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULING
 
17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria 17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria
 
Synchronization hardware
Synchronization hardwareSynchronization hardware
Synchronization hardware
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
 
Os services
Os servicesOs services
Os services
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Resource management
Resource managementResource management
Resource management
 
System call
System callSystem call
System call
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Operating system 08 time sharing and multitasking operating system
Operating system 08 time sharing and multitasking operating systemOperating system 08 time sharing and multitasking operating system
Operating system 08 time sharing and multitasking operating system
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
evolution of operating system
evolution of operating systemevolution of operating system
evolution of operating system
 

Similar to Monitors

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communication
Rai University
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
sgpraju
 
Operating system 26 monitors
Operating system 26 monitorsOperating system 26 monitors
Operating system 26 monitors
Vaibhav Khanna
 
Monitor(karthika)
Monitor(karthika)Monitor(karthika)
Monitor(karthika)Nagarajan
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3  synchronizationOS Process synchronization Unit3  synchronization
OS Process synchronization Unit3 synchronization
subhamchy2005
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
Os3 2
Os3 2Os3 2
Os3 2issbp
 
Operating System
Operating SystemOperating System
Operating System
Subhasis Dash
 
Synchronization
SynchronizationSynchronization
SynchronizationMohd Arif
 
A beginner's guide to eventloops in node
A beginner's guide to eventloops in nodeA beginner's guide to eventloops in node
A beginner's guide to eventloops in node
Fibonalabs
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.ppt
wafawafa52
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
ImranKhan880955
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
ssuserf67e3a
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
Mixer Ingredient Feed
Mixer Ingredient FeedMixer Ingredient Feed
Mixer Ingredient Feed
msindex
 

Similar to Monitors (20)

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communication
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
Operating system 26 monitors
Operating system 26 monitorsOperating system 26 monitors
Operating system 26 monitors
 
Scheduling
SchedulingScheduling
Scheduling
 
Os3
Os3Os3
Os3
 
Monitor(karthika)
Monitor(karthika)Monitor(karthika)
Monitor(karthika)
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3  synchronizationOS Process synchronization Unit3  synchronization
OS Process synchronization Unit3 synchronization
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Os3 2
Os3 2Os3 2
Os3 2
 
Operating System
Operating SystemOperating System
Operating System
 
Synchronization
SynchronizationSynchronization
Synchronization
 
A beginner's guide to eventloops in node
A beginner's guide to eventloops in nodeA beginner's guide to eventloops in node
A beginner's guide to eventloops in node
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.ppt
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
OSCh7
OSCh7OSCh7
OSCh7
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Mixer Ingredient Feed
Mixer Ingredient FeedMixer Ingredient Feed
Mixer Ingredient Feed
 

More from Mohd Arif

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcpMohd Arif
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarpMohd Arif
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocolMohd Arif
 
Project identification
Project identificationProject identification
Project identificationMohd Arif
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniquesMohd Arif
 
Presentation
PresentationPresentation
PresentationMohd Arif
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peerMohd Arif
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systemsMohd Arif
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdpMohd Arif
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgetingMohd Arif
 
Network management
Network managementNetwork management
Network managementMohd Arif
 
Networing basics
Networing basicsNetworing basics
Networing basicsMohd Arif
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformMohd Arif
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and sslMohd Arif
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psecMohd Arif
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardwareMohd Arif
 

More from Mohd Arif (20)

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcp
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
 
Project identification
Project identificationProject identification
Project identification
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
 
Presentation
PresentationPresentation
Presentation
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
 
Network management
Network managementNetwork management
Network management
 
Networing basics
Networing basicsNetworing basics
Networing basics
 
Loaders
LoadersLoaders
Loaders
 
Lists
ListsLists
Lists
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platform
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
 
Heap sort
Heap sortHeap sort
Heap sort
 

Recently uploaded

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 

Recently uploaded (20)

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 

Monitors

  • 1. What Is a Monitor? - Basics qMonitor is a highly structured programming- language construct. It consists of vPrivate variables and private procedures that can only be used within a monitor. vConstructors that initialize the monitor. vA number of (public) monitor procedures that can be invoked by users. qNote that monitors have no public data. qA monitor is a mini-OS with monitor procedures as system calls. 1
  • 2. Monitor: Mutual Exclusion 1/2 qNo more than one process can be executing within a monitor. Thus, mutual exclusion is guaranteed within a monitor. qWhen a process calls a monitor procedure and enters the monitor successfully, it is the only process executing in the monitor. qWhen a process calls a monitor procedure and the monitor has a process running, the caller will be blocked outside of the monitor. 2
  • 3. Monitor: Mutual Exclusion 2/2 processes waiting qIf there is a process to enter monitor executing in a monitor, any process that calls a monitor procedure is blocked outside of the monitor. qWhen the monitor has no executing process, one process will be let in. 3
  • 4. Monitor: Syntax monitor Monitor-Name qAll variables are private. { Why? Exercise! local variable declarations; qMonitor procedures are Procedure1(…) public; however, some { // statements }; procedures can be made Procedure2(…) private so that they can { // statements }; only be used within a // other procedures { monitor. // initialization qInitialization procedures } (i.e., constructors) } execute only once when the monitor is created. 4
  • 5. Monitor: A Very Simple Example monitor IncDec process Increment { while (1) { int count; // do something void Increase(void) IncDec.Increase(); { count++; } cout << IncDec.GetData(); void Decrease(void) // do something { count--; } } int GetData(void) { return count; } { count = 0; } initialization } 5
  • 6. Condition Variables qWith monitors, mutual exclusion is an easy task. qWhile the process is executing within a monitor, a programmer may want to block this process and force it to wait until an event occurs. qThus, each programmer-defined event is artificially associated with a condition variable. 6 qA condition variable, or a condition, has a
  • 7. Condition wait qLet cv be a condition variable. The use of methods signal and wait on cv are cv.signal() and cv.wait(). qCondition wait and condition signal can only be used within a monitor. qA process that executes a condition wait blocks immediately and is put into the waiting list of that condition variable. qThis means that this process is waiting for the indicated event to occur. 7
  • 8. Condition signal qCondition signal is used to indicate an event has occurred. qIf there are processes waiting on the signaled condition variable, one of them will be released. qIf there is no waiting process waiting on the signaled condition variable, this signal is lost as if it never occurs. qConsider the released process (from the signaled condition) and the process that signals. There are two processes executing in the monitor, and mutual exclusion is violated! 8
  • 9. Two Types of Monitors qAfter a signal, the released process and the signaling process may be executing in the monitor. qThere are two common and popular approaches to address this problem: vHoare Type (proposed by C.A.R.Hoare): The released process takes the monitor and the signaling process waits somewhere. vMesa Type (proposed by Lampson and Redell): The released process waits somewhere and the signaling process continues to use the monitor. 9
  • 10. What do you mean by “waiting somewhere”? qThe signaling process (Hoare type) or the released process (Mesa type) must wait somewhere. qYou could consider there is a waiting bench in a monitor for these processes to wait. qAs a result, each process that involves in a monitor call can be in one of the four states: vActive: The running one vEntering: Those blocked by the monitor vWaiting: Those waiting on a condition variable vInactive: Those waiting on the waiting bench 10
  • 11. Monitor with Condition Variables qProcesses suspended due to signal/wait are in the Re-entry list (i.e., waiting bench). qWhen the monitor is free, a process is released from either incoming or re-entry . 11
  • 12. What is the major difference? Condition UntilHappen; With Hoare type, once a signal arrives, the signaler // Hoare Type has yielded the monitor to the if (!event) released process and the UntilHappen.wait(); condition is not changed. Thus, a if is sufficient. // Mesa Type while (!event) With Mesa type, the released UntilHappen.wait(); process may be suspended for a while before it runs. During this period, other processes may be in the monitor and change the condition. It is better to check the condition again with a while! 12
  • 13. Monitor: Dining Philosophers Revisited qInstead of picking up chopsticks one by one, we insist that a philosopher can eat only if he can pick up both simultaneously. qCan we use a semaphore to protect chopsticks 0 and 1, another for 1 and 2, and so on? No, no, no. qRace condition!!!!! 13
  • 14. Monitor Definition monitor Control int CanEat(int i) { { bool used[5]; if (!Used[i] && condition self[5]; !Used[(i+1)%5]) private: return TRUE; int CanEat(int); else return FALSE; procedure GET(int); } procedure PUT(int); { // initialization Function CanEat() returns for (i=0;i<5;i++) TRUE if both chops for used[i] = FALSE; Philosopher i are available. } 14 }
  • 15. Monitor: GET() and PUT() Why a while rather than a if even with a Hoare monitor? void GET(int i) void PUT(int i) { { while (!CanEat(i)) Used[i] = FALSE; self[i].wait(); Used[(i+1)%5] Used[i] = TRUE; = FALSE; Used[(i+1)%5] = TRUE; for (i=0;i<5;i++) } self[i].signal(); } qIn fact, PUT() only requires to signal self[(i+1)%5] and self[(i+4)%5], the two neighbors of philosopher i. qDoes it really matter? Why? 15
  • 16. Monitor: Producer/Consumer monitor ProdCons { int count, in, out; int Buf[SIZE]; condition UntilFull, UntilEmpty; procedure PUT(int); procedure GET(int *); { count = 0} bounded-buffer } 16
  • 17. Monitor: PUT() and GET() void PUT(int X) void GET(int *X) { { if (count == SIZE) if (count == 0) UntilEmpty.wait(); UntilFull.wait(); Buf[in] = X; *X = Buf[out]; in = (in+1)%SIZE; out=(out+1)%SIZE; count++; count--; if (count == 1) if (count == SIZE-1) UntilFull.signal(); UntilEmpty.signal(); } } qChange if to while for Mesa type monitors. 17
  • 18. Dining Philosophers: Again! qIn addition to thinking and eating, a philosopher has one more state, hungry, in which he is trying to get chops. qWe use an array state[] to keep track the state of a philosopher. Thus, philosopher i can eat (i.e., state[i] = EATING) only if his neighbors are not eating (i.e., state[(i+4)%5] and state[(i+1)%5] are not EATING). 18
  • 19. Monitor Definition monitor philosopher { enum { THINKING,HUNGRY, EATING} state[5]; condition self[5]; private: test(int); procedure GET(int); procedure PUT(int); { for (i=0;i<5;i++) state[i] = THINKING; } } 19
  • 20. The test() Procedure the left and right neighbors of void test(int k) philosopher k are not eating { if ((state[(k+4)%5] != EATING) && (state[k] == HUNGRY) && (state[(k+1)%5] != EATING)) { philosopher state[k] = EATING; k is hungry self[k].signal(); } } qIf the left and right neighbors of philosopher k are not eating and philosopher k is hungry, then philosopher k can eat. Thus, release him! 20
  • 21. The GET() and PUT() Procedures I am hungry void GET(int i) { see if I can eat state[i] = HUNGRY; If I could not eat, test(i); Then block myself if (state[i] != EATING) self[i].wait(); } I finished eating void PUT(int i) { Let my neighbors state[i] = THINKING; use my chops test((i+4) % 5); test((i+1) % 5); } Which type of monitor am I using? 21
  • 22. How about Deadlock? void test(int k) { if ((state[(k+4)%5] != EATING) && (state[k] == HUNGRY) && (state[(k+1)%5] != EATING)) { state[k] = EATING; self[k].signal(); } } qThis solution does not have deadlock, because vThe only place where eating permission is granted is in procedure test(), and ….. vPhilosopher k can eat only if his neighbors are not eating. Thus, no two neighboring philosophers can eat at the same time. 22
  • 23. Hoare Type vs. Mesa Type qWhen a signal occurs, Hoare type monitor uses two context switches, one switching the signaling process out and the other switching the released in. However, Mesa type monitor uses one. qProcess scheduling must be very reliable with Hoare type monitors to ensure once the signaling process is switched out the next one must be the released process. Why? qWith Mesa type monitors, a condition may be evaluated multiple times. However, incorrect signals will do less harm because every process checks its own condition. 23
  • 24. Semaphore vs. Condition Semaphores Condition Variables Can be used anywhere, but Can only be used in monitors not in a monitor wait() does not always wait() always blocks its block its caller caller signal() either releases a signal() either releases a process, or increases the process, or the signal is lost as semaphore counter if it never occurs If signal() releases a If signal() releases a process, the caller and the process, either the caller or released both continue the released continues, but not both 24