Title: Monitor%20(Hoare)
1Monitor (Hoare)
- Collection of Data Structures and procedures
- Has a special varible called a CONDITION
VARIABLE - A Condition Variable has two separate operators
that can operate on it
2Condition Variable
- Example
- Condition variable AOK
- AOK.wait (causes the signaling process to go to
sleep) - AOK.signal (causes the process at the beginning
of the queue associated with the condition
variable to awaken and begin executing IMMEDIATELY
3MONITORS
- Important points to remember
- Vs are remembered, signals are not
- Only one process may be in the monitor at a time
4Monitor format
- MONITOR synch
- CONDITION aok
- Request A
- If --list ME conditions here
- Then aok.wait
5Monitor format (cont)
- Release A
- --increment counters, check booleans etc
- --decide who can start
- Aok.signal
6Reader/ Writer with Monitors 1
Smallest possible solutionReadWrite Monitorvar
Rok, Wok Condition RR Integer ? 0 number
readers waiting or active WBusy Boolean ?
FALSE
ReqReaderIf (WBusy) Then Begin RR Rok.wa
it EndElse RRRok.signal
ReqWriterIf (RR gt 0 OR WBusy)Then
Wok.waitWBusy TRUE
RelReaderRR--If(RR0)Then Wok.signal
RelWriterWBusy FALSEif(RR0) Then
Wok.signal Else Rok.signal
7Reader/ Writer with Monitors 2
Give priority to writers over readersReadWrite M
onitorvar Rok, Wok Condition WW, RR
Integer ? 0 WBusy Boolean ? FALSE
ReqReaderIf (WWgt0 OR WBusy) Then
Rok.waitRRRok.signal
ReqWriterIf (RR gt 0 OR WBusy)Then Begin
WW Wok.wait WW--EndWBusy TRUE
RelReaderRR--If(RR0)Then Wok.signal
RelWriterWBusy FALSEif(WW0) Then
Wok.signal Else Rok.signal
8General Process Example
3 Types of Processes A,B,CCritical Resource
TCriteria ME B C ME A C ME among the
As ME among the Cs Cs have priority over As
and Bs ABC MonitorVar Aok, Bok, Cok
Condition WC, RB Integer ? 0 ABusy, CBusy
Boolean ? FALSE
9Monitor Example (contd)
ReqAIf(wc gt0 OR ABusy OR CBusy) Then
Aok.waitAbusy TRUE
RelAAbusy FALSEIf(wc0) Then
Aok.signal Else if(RB0) Then Cok.signal
ReqBIf(wc gt 0 OR CBusy) Then Begin
WB Bok.wait WB-- EndRB
RelBRB--if(RB 0 AND NOT Abusy) Then
Cok.signal
10Monitor Example (contd)
RegCIf (RBgt0 OR CBusy OR ABusy)Then
Begin WC Cok.wait WC--EndCbusy
TRUE
RelCCBusy FALSEIf(WC gt 0) Then
Cok.signal Else Begin Aok.signal While
(WB gt 0) Do Bok.signal End
11General Process Example 2
3 Types of Processes A, B, C Critical Resource
T Criteria ME A B ME B C At most 10 As
can run together ME among the Bs As have
priority over Bs Solving using 1)
Semaphores 2) Monitors
12Semaphore Solution
Process A
Process B
Process C
P(StopB)V(StopB)P(MEBC)P(MEAB)P(MEB)ltCSgt
V(MEB)V(MEAB)V(MEBC)
P(SemRC)RCIf(RC 1) Then
P(MEBC)V(SemRC)ltCSgtP(SemRC)RC--If(RC0)
Then V(MEBC)V(SemRC)
P(SemWA)WAIf(WA1) ThenBegin P(StopB)
P(MEAB) EndV(SemWA)P(MEA)ltCSgtV(MEA)P(S
emWA)WA--If(WA0) ThenBegin V(StopB) V
(MEAB) EndV(SemWA)
VarMEAB, MEBC, MEB semaphore ? 1MEA
semaphore ? 10SemWA, SemRC, StopB semaphore ?
1WA, RC integer ? 0
13Monitor Solution
Var Aok, Bok, Cok conditionBBusy Boolean ?
FALSERA, WA, WB, RC, WC integer ?0
ReqAIf(BBUsy OR RA 10)
ThenBegin WA Aok.wait WA-- EndRA
RelARA --If (WA gt 0) Then Aok.signal
Else If (RC 0 AND RA0) Then Bok.signal
14Monitor Solution (Contd)
RegBIf(BBusy OR RA gt0 OR RC gt0 OR WAgt0)
ThenBok.wait BBusy TRUE
RelBBBusy FALSEIf(WA gt0)
ThenBegin While(RAlt10 AND WA gt0)
Aok.signal While(WCgt0) Cok.signal
EndElse if(WCgt0) ThenWhile(WCgt0) Cok.sign
alElse Bok.signal
15Monitor Solution (Contd)
RegCIf(BBusy) ThenBok.wait WC Cok.wait
WC-- End RC
RelCRC--If(RC 0) AND RA 0) Then
Bok.signal