150 likes | 366 Views
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
E N D
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 • 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
MONITORS • Important points to remember: • V’s are remembered, signals are not • Only one process may be in the monitor at a time
Monitor format • MONITOR synch • CONDITION : aok; Request A If --list ME conditions here Then aok.wait
Monitor format (con’t) • Release A --increment counters, check booleans etc --decide who can start Aok.signal
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
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
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
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;
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;
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
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;
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
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
Monitor Solution (Contd) RegCIf(BBusy) ThenBok.wait; WC++; Cok.wait; WC--; End; RC++; RelCRC--;If(RC = 0) AND RA = 0) Then Bok.signal;