type buffer = monitor "bounded buffer monitor" var saved: integer; "saved item is an integer" fullq, emptyq: queue; "used by only two processes" full: boolean; "true if an item is saved:" procedure entry put (item: integer); "puts item in buffer" begin if full then delay(fullq); "block if full" saved := item; "save the item" full := true; "mark as full" continue(emptyq) "unblock consumer" end; procedure entry get(var item: integer); "gets item from the buffer" begin if not full then delay(emptyq); "block if empty" item := saved; "get the item" full := false; "mark as not full" continue(fullq) "unblock producer" end; begin "initialize the monitor" full := false end; producer = process(pass: buffer); "producer uses a buffer" var item: integer; begin cycle "execute in a loop forever" "produce an item" pass.put(item) "pass an item to the monitor" end end; consumer = process(pass: buffer); "consumer uses a buffer" var item: integer; begin cycle pass.get(item); "get an item from the monitor" "consume the item" end end; "declare instances of the monitor, producer, and consumer" "give the producer and consumer access to the monitor" var pass: buffer; prod: producer; cons: consumer; begin init pass, "initialize the monitor" prod(pass), "start the producer process" cons(pass) "start the consumer process" end.