SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Advanced Operating System- Introduction
Advanced Operating System- IntroductionAdvanced Operating System- Introduction
Advanced Operating System- IntroductionDebasis Das
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's typesNishant Joshi
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionBipul Chandra Kar
 
Unit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process SynchronizationUnit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process Synchronizationcscarcas
 
Allocation of Frames & Thrashing
Allocation of Frames & ThrashingAllocation of Frames & Thrashing
Allocation of Frames & Thrashingarifmollick8578
 
Parallel processing (simd and mimd)
Parallel processing (simd and mimd)Parallel processing (simd and mimd)
Parallel processing (simd and mimd)Bhavik Vashi
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationVaibhav Khanna
 
Load Balancing In Distributed Computing
Load Balancing In Distributed ComputingLoad Balancing In Distributed Computing
Load Balancing In Distributed ComputingRicha Singh
 
Producer consumer
Producer consumerProducer consumer
Producer consumerMohd Tousif
 
Operating System - Types Of Operating System Unit-1
Operating System - Types Of Operating System Unit-1Operating System - Types Of Operating System Unit-1
Operating System - Types Of Operating System Unit-1abhinav baba
 

La actualidad más candente (20)

Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Advanced Operating System- Introduction
Advanced Operating System- IntroductionAdvanced Operating System- Introduction
Advanced Operating System- Introduction
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Priority scheduling algorithms
Priority scheduling algorithmsPriority scheduling algorithms
Priority scheduling algorithms
 
Unit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process SynchronizationUnit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process Synchronization
 
Allocation of Frames & Thrashing
Allocation of Frames & ThrashingAllocation of Frames & Thrashing
Allocation of Frames & Thrashing
 
Parallel processing (simd and mimd)
Parallel processing (simd and mimd)Parallel processing (simd and mimd)
Parallel processing (simd and mimd)
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronization
 
Dining philosopher
Dining philosopherDining philosopher
Dining philosopher
 
Process threads operating system.
Process threads operating system.Process threads operating system.
Process threads operating system.
 
Load Balancing In Distributed Computing
Load Balancing In Distributed ComputingLoad Balancing In Distributed Computing
Load Balancing In Distributed Computing
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
 
Operating System - Types Of Operating System Unit-1
Operating System - Types Of Operating System Unit-1Operating System - Types Of Operating System Unit-1
Operating System - Types Of Operating System Unit-1
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
Code generation
Code generationCode generation
Code generation
 

Similar a 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 & communicationRai University
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
Operating system 26 monitors
Operating system 26 monitorsOperating system 26 monitors
Operating system 26 monitorsVaibhav Khanna
 
Monitor(karthika)
Monitor(karthika)Monitor(karthika)
Monitor(karthika)Nagarajan
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
Os3 2
Os3 2Os3 2
Os3 2issbp
 
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 nodeFibonalabs
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.pptwafawafa52
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).pptssuserf67e3a
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
 
Mixer Ingredient Feed
Mixer Ingredient FeedMixer Ingredient Feed
Mixer Ingredient Feedmsindex
 

Similar a 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)
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Os3 2
Os3 2Os3 2
Os3 2
 
Operating System
Operating SystemOperating System
Operating System
 
Mutual exclusion and sync
Mutual exclusion and syncMutual exclusion and sync
Mutual exclusion and sync
 
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
 
OSCh7
OSCh7OSCh7
OSCh7
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
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
 

Más de 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
 

Más de 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
 

Último

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Último (20)

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

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