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.

Mechanisms 2691 procedure Down(var S : Semaphore);2 begin3 BeginRegion(S.MutEx); { use Test<strong>An</strong>dSet }4 if S.Value = 0 then5 Block(S.Waiters); { proceed when unblocked later }6 else7 S.Value := S.Value − 1;8 end;9 EndRegion(S.MutEx);10 end Down;1112 procedure Up(var S : Semaphore);13 begin14 BeginRegion(S.MutEx);15 if not Empty(S.Waiters) then16 UnBlock(S.Waiters) { Unblock one waiter. We continue. }17 else18 S.Value := S.Value + 1;19 end;20 EndRegion(S.MutEx);21 end Up;It is important that Down and Up be mutually exclusive. In particular, any accessof the Value or the Waiters field must be atomic. That is why we use the MutEx field ofeach semaphore to make sure that Down and Up exclude each other. We must make surethat both Down and Up retain exclusion for only a short time; otherwise our attempt toavoid busy waiting has failed.In the simple case, Down does not find S.Value equal to 0 in line 4 and need notblock in line 5. If it does block, we require that the Block routine (in the scheduler)resume some other runnable activity and that Block turn off exclusion before resumption.Block accomplishes this by calling EndRegion(S.MutEx). Block therefore needs toknow which semaphore is in use; we omit this argument for clarity. UnBlock, called byUp in line 16, marks one waiting activity as runnable (for example, by placing it in aready list). The releasing activity then continues and soon releases exclusion at line 20.When the newly runnable activity is scheduled to run, it is again given exclusion, and itfinds itself running at line 5 in the Down routine. It will soon release exclusion itself inline 9.There is some controversy over whether the scheduler should switch immediatelyto the waiting activity that is activated in line 16. Immediate switch guarantees thatwhatever condition is being awaited by that activity still holds, since the Up operationhas just been called and no other activity has had a chance to run. The disadvantage ofan immediate switch is that it tends to increase the total number of switches. The activitythat called Up is likely to call Down for a new region soon, causing it to block in anycase. The Hysteresis Principle suggests that the current process should be allowed tocontinue.Semaphores are so useful that some operating systems provide service calls so thatprocesses that share resources (particularly parts of virtual store) can synchronize theiraccesses. Four service calls are needed:SemaphoreCreate(initial value). This call returns a new semaphore descriptor(a small integer) that the calling process may use. The semaphore structure itself isprotected in the kernel and has its Value field set to the given initial value. Thissemaphore may be inherited by the children of the calling process so they can all

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

Saved successfully!

Ooh no, something went wrong!