250 likes | 412 Views
CIS 720. Invariant Based Methodology. Producer/consumer problem. co Producer: Consumer: do true do true produce pdata buf = pdata cdata = buf; consume cdata
E N D
CIS 720 Invariant Based Methodology
Producer/consumer problem co Producer: Consumer: do true do true produce pdata buf = pdata cdata = buf; consume cdata od od oc
Producer/consumer problem in1 = out1 = in2 = out2 = 0; co Producer: Consumer: do true {in1=out1}do true {in2=out2} produce pdata in1 = in1 + 1 in2 = in2 + 1 buf = pdata{in1 = out1+1}cdata = buf{in2 = out2+1} out1 = out1 + 1 out2 = out2 + 1 {in1=out1} consume cdata{in2 = out2} od od oc Invariant:
Producer/consumer problem co Producer: Consumer: do true do true produce pdata <await(in1 <=out2) in1 = in1 + 1> <await(in2 < out1) in2 = in2 + 1> buf = pdatacdata = buf <out1 = out1 + 1> <out2 = out2 + 1> consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1)
Producer/consumer problem co Producer: Consumer: do true do true produce pdata <await(empty > 0) empty= empty -1><await(full > 0) full = full -1 > buf = pdata cdata = buf <full = full + 1><empty = empty + 1> consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) empty = out2 + 1 in1 full = out1 – in2
Producer/consumer problem co Producer: Consumer: do true do true produce pdata P(empty) P(full) buf = pdata cdata = buf V(full) V(empty) consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) empty = out2 + 1 in1 full = out1 – in2
Producer/consumer problem in1 = out1 = in2 = out2 = 0; co Producer: Consumer1: Consumer2: do true do true do true produce pdata in1 = in1 + 1 in2 = in2 + 1 in3 = in3 + 1 buf = pdata cdata = buf cdata = buf out1 = out1 + 1 out2 = out2 + 1 out3 = out3 + 1 consume cdata consume cdata od od od oc Invariant:
Readers/writers problem • A reader and a writer cannot read and write respectively at the same time. • Two writers cannot write at the same time.
Readers/writers problem co Reader[i]: Writer[j]: do true do true P(rw) P(rw) read write V(rw) V(rw) od od oc
Readers/writers problem co Reader[i]: Writer: do true do true nr = nr + 1 P(rw) if nr == 1 P(rw) fi write read nr = nr - 1 if (nr =0) V(rw) fi V(rw) od od oc
Readers/writers problem co Reader[i]: Writer: do true do true nr=nr+1 nw = nw + 1 read write nr = nr - 1 nw = nw - 1 od od oc
Readers/writers problem co Reader[i]: Writer: do true do true nr=nr+1 nw = nw + 1 read write nr = nr - 1 nw = nw - 1 od od oc BAD = (nr > 0 /\ nw > 0 ) \/ nw > 1 Invariant: not (BAD) (nr = 0 \/ nw = 0) /\ nw <= 1
Readers/writers problem co Reader[i]: Writer: do true do true inr = inr + 1 inw = inw + 1 read write outr = outr + 1 outw = outw + 1 od od oc
Readers/writers problem co Reader[i]: Writer: do true do true inr = inr + 1 inw = inw + 1 read write outr = outr + 1 outw = outw + 1 od od oc BAD = Invariant:
Technique of Passing the baton • Solution may contain statements • F1 : <S> • F2 : < await (Bj) Sj > • Introduce the following semaphores • e : for controlling access to evaluate the conditions • bj : for each guard Bj
Introduce a new counter dj for each guard Bj • Replace as follows: • F1: P(e) Sj SIGNAL
Readers/writers problem co Reader[i]: Writer: do true do true <await(nw = 0) nr=nr+1> <await(nw=0 /\ nr = 0) nw = nw + 1> read write <nr = nr – 1><nw = nw – 1> od od oc
Readers/writers problem co Reader[i]: do true <await(nw = 0) nr=nr+1> read <nr = nr – 1> od oc
Readers/writers problem co Writer: do true <await(nw=0 /\ nr = 0) nw = nw + 1> write <nw = nw – 1> od oc
SIGNAL if (nw = 0 and dr > 0) dr = dr – 1; V(r) elseif (nw = 0 and nr = 0 and dw > 0) dw = dw – 1; V(w) else V(e)
co Reader[i]: Writer: do true do true P(e) P(e) if (nw > 0) dr++; V(e); P(r) fi if (nw=0 /\ nr = 0) dw++; V(e); P(w) finr++; nw++; SIGNAL SIGNAL read write P(e) P(e) nr--; nw-- SIGNAL SIGNAL od od oc • SIGNAL • if (nw = 0 and dr > 0) dr = dr – 1; V(r) • elseif • (nw = 0 and nr = 0 and dw > 0) dw = dw – 1; V(w) • else V(e)
Weak Reader preference • If a reader A is reading and a reader B and a writer C is waiting then B is given preference.
Strong Reader preference • If a reader B and a writer C is waiting then B is given preference.
Strong Writer preference • If a reader B and a writer C is waiting then C is given preference. • SIGNAL: If nw = 0 /\ dr > 0 /\ dw = 0 dr = dr – 1; V(r) [] nr = 0 /\ nw = 0 /\ dw > 0 dw = dw – 1; V(w); [] else -> V(e) fi