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.

Mechanisms 2872.13 Invariant expressionsThe method of invariant expressions is reminiscent of path expressions. We queue allactivities that try to enter each procedure. <strong>An</strong> invariant expression associated with eachqueue tells when it is permissible to allow the first waiter in that queue into the crowd. Ifthere is no invariant expression for some procedure, activities may enter it at any time.Invariant expressions have a very limited syntax. They may use built-in countersthat record the number of interesting events for any procedure P. There are only fivesuch counters:RequestCount(P) counts the number of requests (since the beginning of time) forprocedure P.StartCount(P) counts the number of times procedure P has actually been started.FinishCount(P) counts the number of times procedure P has been finished.CurrentCount(P) counts the number of current executions of P; it is equivalent toStartCount(P)−FinishCount(P).WaitCount(P) counts the number of waiting executions of P; it is equivalent toRequestCount(P)−StartCount(P).<strong>An</strong> invariant expression has the formexpression comparison constantExpressions are just sums and differences of counters. The comparison can be , ≤, ≥,= or ≠. For example, we might writeWaitCount(P) − RequestCount(Q) > 4We associate such an expression with a procedure as a rule that must hold before weallow an activity to execute that procedure. (This interpretation is not the same as sayingthe rule must hold while the activity is executing the procedure.) The following is a solutionto the multiple producer-consumer problem:1 module BoundedBuffer;23 export GetBuffer, PutBuffer;45 const6 Size = 10; { number of elements in the buffer at once }7 type8 Datum = ... { data type of the contents of the buffer }9 var10 Buffer : array 0:Size−1 of Datum;11 NextIn, NextOut : 0..Size−1;12 { index of next datum to insert, delete }13 invariant PutBuffer14 StartCount(PutBuffer) − FinishCount(GetBuffer) < Size15 { no overproduction }16 CurrentCount(PutBuffer) = 0 { prevent producer conflict }17 invariant GetBuffer18 StartCount(GetBuffer) − FinishCount(PutBuffer) < 019 { no overconsumption }20 CurrentCount(GetBuffer) = 0 { prevent consumer conflict }21

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

Saved successfully!

Ooh no, something went wrong!