13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Mechanisms 265processors, we might not mind this waste of time so much because all activitiesthat can make use of a processor resource might be on machines of their own.Still, a busy-waiting activity consumes main-store cycles, which are shared amongall the processors. Even if other activities could run, the Hysteresis Principle tellsus that busy waiting is still reasonable if the expected waiting time is less than oneprocess-switch time. Very long busy-wait times tend to create a convoyphenomenon, in which many activities end up waiting for the same region. Theentire convoy tends to collide at the next region, too, leading to worse contentionthan might have been expected.This solution forces A and B to alternate entry into their regions. If A needs toenter its region only occasionally but B needs to do so frequently, B will beblocked waiting for the region most of the time, even though A is not currently inthe region.2.4 Non-alternating switchThe principal drawback of the switch method is the strict alternation it imposes onconflicting activities. A non-alternating switch uses a switch variable and two auxiliaryBoolean variables that indicate whether the two activities are currently interested inentering their regions. We will store them in an array for convenience. Here is the codethat implements this store-synchronous method:1 type Activity = (A,B);23 var4 Turn : Activity := A;5 Interested : array [Activity] of Boolean := false;67 procedure BeginRegion(Who : Activity);8 var Other : Activity; { the other activity besides Who }9 begin10 if Who = A then Other := B else Other := A end;11 loop "outer"12 { we exit when we may enter our region }13 Interested[Who] := true;14 repeat { busy wait if our turn, Other interested }15 when not Interested[Other] exit outer;16 until Turn = Other;17 { we should stop objecting; it’s not our turn. }18 Interested[Who] := false;19 while Turn = Other do { busy wait for our turn }20 null;21 end;22 end loop;23 end BeginRegion;2425 procedure EndRegion(Who : Activity);26 begin27 if Who = A then Turn := B else Turn := A end;28 Interested[Who] := false;29 end EndRegion;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!