Monitor%20(Hoare) - PowerPoint PPT Presentation

About This Presentation
Title:

Monitor%20(Hoare)

Description:

... (causes the process at the beginning of the queue associated with the ... Only one process may be in the ... Rok.signal Give priority to writers over ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 16
Provided by: vte81
Learn more at: http://courses.cs.vt.edu
Category:

less

Transcript and Presenter's Notes

Title: Monitor%20(Hoare)


1
Monitor (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

2
Condition 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

3
MONITORS
  • Important points to remember
  • Vs are remembered, signals are not
  • Only one process may be in the monitor at a time

4
Monitor format
  • MONITOR synch
  • CONDITION aok
  • Request A
  • If --list ME conditions here
  • Then aok.wait

5
Monitor format (cont)
  • Release A
  • --increment counters, check booleans etc
  • --decide who can start
  • Aok.signal

6
Reader/ 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
7
Reader/ 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
8
General 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
9
Monitor 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
10
Monitor 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
11
General 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
12
Semaphore 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
13
Monitor 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
14
Monitor 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
15
Monitor Solution (Contd)
RegCIf(BBusy) ThenBok.wait WC Cok.wait
WC-- End RC
RelCRC--If(RC 0) AND RA 0) Then
Bok.signal
Write a Comment
User Comments (0)
About PowerShow.com