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.

262 Concurrency Chapter 82.2 Busy wait on MutExThe method of disabling interrupts lets any activity into a region but prevents any otheractivity from running while the first one is in the region. A different approach is to waitat the entry to a region until it is safe to continue. We can introduce a Boolean variableMutEx (for Mutual Exclusion) that is set to true when an activity is in a region and falseotherwise. We then code BeginRegion and EndRegion as follows:1 var MutEx : Boolean := false;23 procedure BeginRegion;4 begin5 while MutEx do6 null; { that is, do nothing }7 end;8 MutEx := true;9 end BeginRegion;1011 procedure EndRegion(Who : Activity);12 begin13 MutEx := false;14 end EndRegion;Unfortunately, this store-synchronous solution is wrong. If two activities enter BeginRegionat about the same time, they might both get past the loop in line 5 and enter theregion. The problem is that an activity that decides to enter the region at line 5 does notprevent others from entering as well until line 8. By then it may be too late. We will fixthis problem when we look at locks.We introduce this ‘‘solution’’ to demonstrate that even though it fails for mutualexclusion, busy waiting does suffice to implement the synchronization graph of Figure8.1. We will use the following convention: Every activity in the graph is associated witha shared Boolean variable ‘‘Done.’’ For example, A is associated with Done[A]. Wethen build two utility procedures, Finishing and WaitFor:1 var2 Done : array [Activity] of Boolean := false;34 procedure Finishing(Who : Activity);5 begin6 Done[Who] := true;7 end Finishing;89 procedure WaitFor(Whom : Activity);10 begin11 while not Done[Whom] do12 null;13 end;14 end WaitFor;The code for a few selected activities could look like this:

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

Saved successfully!

Ooh no, something went wrong!