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.

284 Concurrency Chapter 81 type2 Semaphore =3 record4 T : sequencer;5 E : eventcount;6 I : integer; { initial value }7 end;89 procedure Down(var S : Semaphore);10 begin11 with S do12 Await(E reaches Ticket(T)−I+1);13 end14 end Down;1516 procedure Up(S : Semaphore);17 begin18 Advance(S.E);19 end Up;The Down procedure gets a ticket (which orders all the waiters) and then waits for theevent count to be advanced as many times as the ticket indicates. There is no need forDown and Up to exclude each other. However, we require that every waiter get a differentticket.We can extend this wait procedure to wait for two semaphores at once. Thedining-philosophers problem, described in Chapter 4, can be solved by such a method.Here is a procedure that waits for two semaphores. It uses a third semaphore, MutualExclusion,for its own purposes.1 var2 MutualExclusion : Semaphore; { initialized at I = 1 }34 procedure DoubleDown(var S1, S2 : Semaphore);5 var6 Ticket1, Ticket2 : integer;7 begin8 Down(MutualExclusion);9 Ticket1 := Ticket(S1.T);10 Ticket2 := Ticket(S2.T);11 Up(MutualExclusion);12 with S1 do13 Await(E reaches Ticket1);14 end;15 with S2 do16 Await(E reaches Ticket2);17 end;18 end DoubleDown;We use the MutualExclusion semaphore in lines 8−11 to make sure that we acquirethe two tickets as an atomic action. It makes no difference in what order we wait for S1and S2 (lines 13 and 16).2.12 Path expressions

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

Saved successfully!

Ooh no, something went wrong!