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.

268 Concurrency Chapter 8It is liberal in that an activity that holds a lock does not prevent another activityfrom acquiring a different lock.It allows starvation because the choice of which activity to honor is arbitrary, onthe assumption that the hardware is unintelligent.It uses busy waiting.2.6 SemaphoresEach of the previous solutions employs busy waiting. The following methods all share anew property that distinguishes them from the busy-waiting methods: They depend onthe ability to schedule activities. <strong>An</strong> activity that attempts to enter a region that is alreadyoccupied can be blocked, just as a process that attempts to gain a resource that iscurrently allocated might be blocked by the resource manager. While it waits, otheractivities may continue. Instead of consuming computational resources in a fruitlessloop, the waiting activity only needs to suffer the cost of a context switch or two. Toachieve this effect, we need to embed the BeginRegion and EndRegion operations in thescheduler instead of building them directly on the hardware. The scheduler can alsomaintain a list of activities waiting to enter a region so that a fair choice can be madewhen conditions allow.Our first example of this approach is to protect each group of shared variables witha semaphore, which has the following structure:1 type2 Queue = list of Activity3 Semaphore =4 record5 { all fields are initialized as shown }6 MutEx : Lock := false;7 Value : integer := 1;8 Waiters : Queue := empty;9 end;The scheduler provides two operations on semaphores. We will call the first Up (sometimespeople call this operation P, Wait, or Acquire). The second operation is Down(also called V, Signal, or Release). Informally, Down blocks the caller if Value (line 7) is0. Otherwise, it decrements Value. Up increments Value and unblocks at most one waitingactivity.The correct use of semaphores to implement mutual exclusion is simple: Allregions that use the same shared variables are associated with the same semaphore. <strong>An</strong>activity that wishes to enter a region calls Down on the associated semaphore (instead ofBeginRegion). When the activity exits the region, it calls Up on the same semaphore(instead of EndRegion). The first activity to try to enter its region succeeds, becauseValue is initially 1. <strong>An</strong>other activity that tries to enter while the first is still in its regionwill be blocked. When the first activity leaves the region, the second activity isunblocked. The Value field is always either 0 or 1.To give a more formal definition of Down and Up, we will implement them usinglocks as a more primitive mutual-exclusion tool.

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

Saved successfully!

Ooh no, something went wrong!