home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
cs.rhul.ac.uk
/
www.cs.rhul.ac.uk.zip
/
www.cs.rhul.ac.uk
/
pub
/
CS375
/
pcmon2.pfc
< prev
next >
Wrap
Text File
|
1999-03-15
|
1KB
|
75 lines
program pcmon2;
(* producer-consumer problem - correct
monitor solution
file is ~simon/teaching/CS375/mutex/pcmon2.pfc *)
monitor buffer;
export
put, take;
const
buffmax = 4;
var
store: array[0..buffmax] of char;
count: integer;
notfull, notempty: condition;
nextin, nextout: integer;
procedure put(ch: char);
begin
if count > buffmax then
delay(notfull);
store[nextin] := ch;
count := count + 1;
nextin := (nextin + 1) mod (buffmax + 1);
resume(notempty)
end; (* put *)
procedure take(var ch: char);
begin
if count = 0 then
delay(notempty);
ch := store[nextout];
count := count - 1;
nextout := (nextout + 1) mod (buffmax + 1);
resume(notfull)
end; (* take *)
begin (* body of buffer *)
count := 0;
nextin := 0;
nextout := 0
end; (* buffer *)
process producer;
var
local: char;
begin
for local := 'a' to 'z' do
buffer.put(local);
end; (* producer *)
process consumer;
var
ch: char;
begin
repeat
buffer.take(ch);
write(ch)
until ch = 'z';
writeln
end; (* consumer *)
begin (* main *)
cobegin
producer;
consumer
coend
end.