1 / 15

Monitor (Hoare)

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. Condition Variable. Example Condition variable : AOK

august
Download Presentation

Monitor (Hoare)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: • V’s 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 (con’t) • 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.wait; End;Else RR+;Rok.signal ReqWriterIf (RR > 0 OR WBusy)Then Wok.wait;WBusy := TRUE; RelReaderRR--;If(RR=0)Then Wok.signal RelWriterWBusy := FALSEif(RR=0) Then Wok.signal Else Rok.signal

  7. Reader/ Writer with Monitors #2 Give priority to writers over readersReadWrite: Monitorvar Rok, Wok: Condition; WW, RR: Integer  0; WBusy: Boolean  FALSE; ReqReaderIf (WW>0 OR WBusy) Then Rok.wait;RR+;Rok.signal ReqWriterIf (RR > 0 OR WBusy)Then Begin WW++; Wok.wait; WW--;End;WBusy := TRUE; RelReaderRR--;If(RR=0)Then Wok.signal RelWriterWBusy := FALSEif(WW=0) 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 A’s ME among the C’s C’s have priority over A’s and B’s ABC MonitorVar Aok, Bok, Cok: Condition WC, RB: Integer  0; ABusy, CBusy: Boolean  FALSE

  9. Monitor Example (contd) ReqAIf(wc >0 OR ABusy OR CBusy) Then Aok.wait;Abusy := TRUE; RelAAbusy := FALSE;If(wc=0) Then Aok.signal Else if(RB=0) Then Cok.signal ReqBIf(wc > 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 (RB>0 OR CBusy OR ABusy)Then Begin WC++; Cok.wait; WC--;End;Cbusy := TRUE; RelCCBusy := FALSE;If(WC > 0) Then Cok.signal Else Begin Aok.signal; While (WB > 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 A’s can run together ME among the B’s A’s have priority over B’s 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)<CS>V(MEB)V(MEAB);V(MEBC); P(SemRC);RC++;If(RC = 1) Then P(MEBC);V(SemRC);<CS>P(SemRC);RC--;If(RC=0) Then V(MEBC);V(SemRC); P(SemWA);WA++;If(WA=1) ThenBegin P(StopB); P(MEAB); End;V(SemWA);P(MEA);<CS>V(MEA)P(SemWA);WA--;If(WA=0) ThenBegin V(StopB); V(MEAB); EndV(SemWA); VarMEAB, MEBC, MEB: semaphore  1;MEA: semaphore  10;SemWA, SemRC, StopB: semaphore  1WA, RC: integer  0;

  13. Monitor Solution Var Aok, Bok, Cok: condition;BBusy: Boolean  FALSE;RA, WA, WB, RC, WC: integer 0; ReqAIf(BBUsy OR RA = 10) ThenBegin WA++; Aok.wait; WA--; End;RA++; RelARA --;If (WA > 0) Then Aok.signal Else If (RC = 0 AND RA=0) Then Bok.signal

  14. Monitor Solution (Contd) RegBIf(BBusy OR RA >0 OR RC >0 OR WA>0) ThenBok.wait; BBusy := TRUE; RelBBBusy := FALSE;If(WA >0) ThenBegin While(RA<10 AND WA >0) Aok.signal; While(WC>0) Cok.signal; EndElse if(WC>0) ThenWhile(WC>0) Cok.signalElse 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;

More Related