home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
cs.rhul.ac.uk
/
www.cs.rhul.ac.uk.zip
/
www.cs.rhul.ac.uk
/
pub
/
CS375
/
philsem1.pfc
< prev
next >
Wrap
Text File
|
1999-03-15
|
696b
|
39 lines
program philsem1;
(* Dining Philosophers - semaphore version 1
File is /CS/fftp/pub/CS375/sems/philsem1.pfc
*)
const
N = 5;
var
fork : array [1..N] of semaphore; (* binary *)
i : integer;
process type philosophers(name : integer);
begin
repeat
sleep(random(3)); (* THINKING *)
wait(fork[name]);
wait(fork[(name mod N) + 1]);
sleep(random(3)); (* EATING *)
writeln(name);
signal(fork[name]);
signal(fork[(name mod N) + 1]);
forever
end; (* philosophers *)
var
phils: array[1..N] of philosophers;
begin
for i := 1 to N do
initial(fork[i],1);
cobegin
for i := 1 to N do
phils[i](i);
coend
end.