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.

264 Concurrency Chapter 8to arbitrate simultaneous accesses to this variable.In the following example, the switch variable is called Turn:1 type Activity : (A, B);23 var Turn : Activity := A;45 procedure BeginRegion(Who : Activity);6 begin7 while Turn Who do8 null;9 end;10 end BeginRegion;1112 procedure EndRegion(Who : Activity);13 begin14 if Who = A then15 Turn := B16 else17 Turn := A;18 end;19 end EndRegion;In line 3, we initialize the switch variable to point to activity A. If A calls BeginRegion,the condition on the while in line 7 will be false, so BeginRegion will return. If B tries toenter its region, it will be blocked waiting for the switch in line 7. Turn will become Bonly after A has released the switch by calling EndRegion.Switches are only slightly better than busy waiting.They correctly allow A and B to execute in conflicting regions, even if A and B areon different machines in a multiprocessor. If A and B call BeginRegion simultaneously,they will not conflict in their use of Turn, since BeginRegion only examinesTurn without changing it. A and B will never call EndRegion simultaneouslybecause they can’t both be in their region at once.They may be generalized to more than two processes.Switches are more liberal than disabling interrupts. Unrelated conflicts are handledby different switch variables. Therefore, two activities can be executing in nonconflictingregions at the same time. For example, assume that there are twoshared variables, x and y, and that activity A uses just x, activity B uses both x andy, and activity C uses just y. We would use one switch variable, TurnX, withvalues A and B, to protect variable x. A different switch variable, TurnY, withvalues B and C, could protect variable y. When B wishes to enter a region, itwould specify not only its own name (B), but also which switch variable it wants touse.If several switch variables exist and a single activity might want to be in a combinedregion protected by several different switch variables, there is a danger ofdeadlock because switch variables are serially reusable resources. A hierarchy ofswitch variables can prevent this problem. We used the same idea in Chapter 4when we introduced hierarchical allocation of sequentially reusable resources.When an activity is waiting to enter its region (line 7), it performs no useful workwhile it consumes computational resources. If activities are competing with eachother on a single processor, these busy waits waste time because no other productiveaction takes place, even though there is some activity that could make use ofthe time (namely, the activity that is in the region). If activities are on different

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

Saved successfully!

Ooh no, something went wrong!