13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

266 Concurrency Chapter 8The Interested variables ensure that simultaneous execution does not occur: Eachactivity indicates that it is interested (line 13) both before it enters its region and duringthe entire time it is in the region. <strong>An</strong> activity enters the region only if the other activity isnot interested. The Turn variable ensures that mutual blocking does not occur. It doesnot change value during BeginRegion, and it is used only to determine which activitybacks off (lines 17−21) if both wish to enter their region. Only activity A ever modifiesInterested[A], and, likewise, only B modifies Interested[B], so there is no conflict forthose variables. Only the activity currently in a region modifies Turn (line 27), so there isno conflict for that variable either.A significant amount of effort has been directed to deriving the non-alternatingswitch. There are many ways in which the switch can be built incorrectly. For manyyears the program just shown, developed by Dekker, was the only good solution. Asurprisingly simple solution was published by Peterson in 1981: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 Interested[Who] := true;12 Turn := Other;13 while Interested[Other] and Turn = Other do14 null;15 end;16 end BeginRegion;1718 procedure EndRegion(Who : Activity);19 begin20 Interested[Who] := false;21 end EndRegion;We leave the proof of this implementation as an exercise.The non-alternating switch has the following properties:It is correct in the same way that the ordinary switch is correct.It is more liberal than the ordinary switch because it allows a faster activity to enterits region many times before the other activity has entered. However, it is still fairbecause priority is given to the activity that has not entered its region for the longesttime.It is impossible for an activity to starve while waiting to enter its region, if weassume that whenever the conflicting activity enters its region, it eventually leavesthat region.The solution can be generalized to any number of activities that conflict over thesame shared variables. The generalization is called a spin switch.The solution is complex and somewhat unclear, so it is easy to make a mistakewhen programming it.The non-alternating switch uses busy waiting if a conflicting activity is in itsregion.

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

Saved successfully!

Ooh no, something went wrong!