Remember the readers and writers problem ?
Process READER loop SEND(dbmanager, startread) ; RECEIVE(dbmanager, msg) ; {wait for ok} {read_database} SEND(dbmanager, endread) ; {use data} end Process WRITER loop {generate data} SEND(dbmanager, startwrite) ; RECEIVE(dbmanager, msg) ; {wait for ok} {write database} SEND(dbmanager, endwrite) ; {use data} end
What does dbmanager do ?
Process DBMANAGER rc = 0 : integer {reader count} writing = false : boolean ; msg : message ; rq, wq : queue {reader, writer queue} src : processid ; loop src := RECEIVEANY(msg) ; CASE msg OF startread : IF NOT writing THEN send(src,ok); rc=rc+1 END ELSE addq(src,rq) {writer busy, Q reader} endread : rc := rc -1 ; IF rc=0 & notempty(wq) THEN {First writer on Q can write} src := removeq(wq); SEND(src,ok); writing=TRUE ; END startwrite : IF rc=0 & NOT writing THEN send(src,ok); writing := TRUE END ELSE addq(src,wq) {database in use} endwrite : writing := FALSE ; IF empty(rq) & notempty(wq) THEN src := removeq(wq) ; send(src,ok); writing := TRUE ; END ELSE WHILE notempty(rq) DO {all queued readers continue} src := removeq(rq) ; send(src,ok) ; rc=rc + 1 ; END END END{case} END {loop}