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.

282 Concurrency Chapter 8 Read(E) returns the value of the event count E. If Read returns some number n,then at least n Advance operations must have happened. By the time this numberis returned, the event count may have been advanced again a number of times. Await(E reaches v) waits for the event count E to have the value v. It blocks thecalling activity until at least v Advance operations have occurred. It is acceptableif more than v Advance operations have occurred when it is finally unblocked.This ‘‘overshoot’’ could result from very frequent Advance operations. (We adoptthe syntax shown here for clarity. Await could be a procedure with two parameters,using a comma instead of reaches.)These definitions allow both Await and Read to be concurrent with Advance, since wedon’t care if Read gives us a somewhat stale value or if Await waits a trifle too long.We introduced the producers-consumers problem earlier to show how monitorshandle synchronization. Here is a solution using event counts. For the time being, weassume that there is only one producer and one consumer.1 const2 Size = 10; { number of elements in the buffer. }3 type4 Datum = ... { data type for buffer contents. }5 var6 In, Out : eventcount;7 Buffer : array 0:Size-1 of Datum;89 activity Producer;10 var11 SequenceNumber : integer;12 Value : Datum;13 begin14 for SequenceNumber := 1 to infinity do15 ... { compute Value }16 Await(Out reaches SequenceNumber − Size);17 Buffer[SequenceNumber mod Size] := Value;18 Advance(In)19 end20 end Producer;2122 activity Consumer;23 var24 SequenceNumber : integer;25 Value : Datum;26 begin27 for SequenceNumber := 1 to infinity do28 Await(In reaches SequenceNumber);29 Value := Buffer[SequenceNumber mod Size];30 Advance(Out)31 ... { use the Value }32 end33 end Consumer;There is no need for us to worry that the consumer and producer will simultaneouslyaccess the same cell in Buffer, since the producer will have to wait until the consumerhas used that cell before its Await in line 16 will allow it to proceed. Similarly, the consumerknows that when it accesses a cell of Buffer, the producer must have placed datathere, or the Await in line 28 would not have unblocked. In addition, we need not worrythat both Advance operations (lines 18 and 30) will happen at the same time, because

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

Saved successfully!

Ooh no, something went wrong!