resource bounded_buffer op deposit(item : real) op fetch() returns item : real body bounded_buffer(size : int) var buf[0:size-1] : real var count : int := 0 var take_out : int := 0 var put_in : int := 0 sem elements := 0 sem spaces := size sem mutex := 1 write("bounded buffer resource with", size, "slots is alive") proc deposit(item) P(spaces) buf[put_in] := item put_in := (put_in + 1) % size P(mutex) count++ write("deposit: there are now", count, "items in the buffer") V(mutex) V(elements) end deposit proc fetch() returns item P(elements) item := buf[take_out] take_out := (take_out + 1) % size P(mutex) count-- write("fetch: there are now", count, "items in the buffer") V(mutex) V(spaces) end fetch end bounded_buffer resource user() import bounded_buffer var bb : cap bounded_buffer var slots : int := 20, pnap : int := 3, cnap : int := 3 var run_time : int := 60 getarg(1, slots); getarg(2, pnap); getarg(3, cnap); getarg(4, run_time) writes("main user resource alive, slots = ", slots, ", pnap = ", pnap, ", cnap = ", cnap, ", and run_time = ", run_time, "\n") bb := create bounded_buffer(slots) process producer var it : real var napping : int do true -> it := random() writes("age=", age(), ", PRODUCER produced item ", it, "\n") bb.deposit(it) napping := int(random(1000*pnap)) writes("age=", age(), ", PRODUCER deposited item ", it, " and napping for ", napping, "ms\n") nap(napping) od end producer process consumer var it : real var napping : int do true -> writes("age=", age(), ", consumer wants to consume ...\n") it := bb.fetch() napping := int(random(1000*cnap)) writes("age=", age(), ", consumer fetched item ", it, " and napping for ", napping, "ms\n") nap(napping) od end consumer nap(1000*run_time); write("must stop now"); stop end user /* ............... Example compile and run(s) % sr -o bb bb.sr % ./bb 5 1 3 10 main user resource alive, slots = 5, pnap = 1, cnap = 3, and run_time = 10 bounded buffer resource with 5 slots is alive age=50, consumer wants to consume ... age=53, PRODUCER produced item 0.356596 deposit: there are now 1 items in the buffer age=66, PRODUCER deposited item 0.356596 and napping for 364ms fetch: there are now 0 items in the buffer age=73, consumer fetched item 0.356596 and napping for 316ms age=418, consumer wants to consume ... age=449, PRODUCER produced item 0.00800780 deposit: there are now 1 items in the buffer age=456, PRODUCER deposited item 0.00800780 and napping for 654ms fetch: there are now 0 items in the buffer age=464, consumer fetched item 0.00800780 and napping for 2152ms age=1118, PRODUCER produced item 0.755083 deposit: there are now 1 items in the buffer age=1131, PRODUCER deposited item 0.755083 and napping for 19ms age=1158, PRODUCER produced item 0.339312 deposit: there are now 2 items in the buffer age=1165, PRODUCER deposited item 0.339312 and napping for 138ms age=1308, PRODUCER produced item 0.212412 deposit: there are now 3 items in the buffer age=1316, PRODUCER deposited item 0.212412 and napping for 567ms age=1888, PRODUCER produced item 0.00888645 deposit: there are now 4 items in the buffer age=1894, PRODUCER deposited item 0.00888645 and napping for 324ms age=2229, PRODUCER produced item 0.204411 deposit: there are now 5 items in the buffer age=2253, PRODUCER deposited item 0.204411 and napping for 501ms age=2629, consumer wants to consume ... fetch: there are now 4 items in the buffer age=2636, consumer fetched item 0.755083 and napping for 2215ms age=2759, PRODUCER produced item 0.727478 deposit: there are now 5 items in the buffer age=2818, PRODUCER deposited item 0.727478 and napping for 707ms age=3539, PRODUCER produced item 0.569443 age=4860, consumer wants to consume ... fetch: there are now 4 items in the buffer age=4864, consumer fetched item 0.339312 and napping for 1523ms deposit: there are now 5 items in the buffer age=4868, PRODUCER deposited item 0.569443 and napping for 732ms age=5608, PRODUCER produced item 0.989578 age=6398, consumer wants to consume ... fetch: there are now 4 items in the buffer age=6407, consumer fetched item 0.212412 and napping for 2838ms deposit: there are now 5 items in the buffer age=6411, PRODUCER deposited item 0.989578 and napping for 462ms age=6879, PRODUCER produced item 0.996745 age=9362, consumer wants to consume ... fetch: there are now 4 items in the buffer age=9371, consumer fetched item 0.00888645 and napping for 1097ms deposit: there are now 5 items in the buffer age=9375, PRODUCER deposited item 0.996745 and napping for 68ms age=9448, PRODUCER produced item 0.0306415 must stop now % ./bb 5 3 1 10 main user resource alive, slots = 5, pnap = 3, cnap = 1, and run_time = 10 bounded buffer resource with 5 slots is alive age=73, consumer wants to consume ... age=77, PRODUCER produced item 0.382618 deposit: there are now 1 items in the buffer age=88, PRODUCER deposited item 0.382618 and napping for 1857ms fetch: there are now 0 items in the buffer age=96, consumer fetched item 0.382618 and napping for 867ms age=980, consumer wants to consume ... age=1950, PRODUCER produced item 0.0659844 deposit: there are now 1 items in the buffer age=1956, PRODUCER deposited item 0.0659844 and napping for 133ms fetch: there are now 0 items in the buffer age=1960, consumer fetched item 0.0659844 and napping for 669ms age=2100, PRODUCER produced item 0.714716 deposit: there are now 1 items in the buffer age=2111, PRODUCER deposited item 0.714716 and napping for 2716ms age=2640, consumer wants to consume ... fetch: there are now 0 items in the buffer age=2647, consumer fetched item 0.714716 and napping for 517ms age=3180, consumer wants to consume ... age=4840, PRODUCER produced item 0.653636 deposit: there are now 1 items in the buffer age=4846, PRODUCER deposited item 0.653636 and napping for 2656ms fetch: there are now 0 items in the buffer age=4849, consumer fetched item 0.653636 and napping for 927ms age=5780, consumer wants to consume ... age=7510, PRODUCER produced item 0.992305 deposit: there are now 1 items in the buffer age=7516, PRODUCER deposited item 0.992305 and napping for 2629ms fetch: there are now 0 items in the buffer age=7520, consumer fetched item 0.992305 and napping for 76ms age=7600, consumer wants to consume ... must stop now */