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