Section 18.5 three foils on solutions to dining philosophers problem
(see solutions to exercises)
A first attempt (with deadlock):
repeat
think
WAIT (fork(i));
WAIT (fork(i@+1));
eat
SIGNAL (fork(i));
SIGNAL (fork(i@+1))
until false;
A solution:
guard: semaphore := 1
repeat
think
WAIT (guard); % put a critical region round
WAIT (fork(i)); % the two WAIT (fork) operations
WAIT (fork(i@+1));
SIGNAL (guard);
eat
SIGNAL (fork(i));
SIGNAL (fork(i @+ 1))
until false;
Another solution:
count: semaphore := 4
repeat
think
WAIT (count); % only 4 at once can pass this point
WAIT (fork(i));
WAIT (fork(i@+1));
eat
SIGNAL (fork(i));
SIGNAL (fork(i@+1));
SIGNAL (count);
until false;