Page 112 - DCAP403_Operating System
P. 112
Unit 6: Process Synchronization
end left Notes
procedure right(i : int) returns rgh : int
if i=num_phil -> rgh := 1 [] else -> rgh := i+1 fi
end right
procedure test(i : int)
if state[i] = hungry and state[left(i)] ~= eating
and state[right(i)] ~= eating ->
state[i] := eating
V(phil[i])
fi
end test
proc take_forks(i)
P(mutex)
state[i] := hungry
test(i)
V(mutex)
P(phil[i])
end take_forks
proc put_forks(i)
P(mutex)
state[i] := thinking
test(left(i)); test(right(i))
V(mutex)
end put_forks
end dining_server
resource start()
import philosopher, dining_server
var num_phil : int := 5, run_time : int := 60
getarg(1, num_phil); getarg(2, run_time)
var max_think_delay[1:num_phil] : int := ([num_phil] 5)
var max_eat_delay[1:num_phil] : int := ([num_phil] 2)
fa i := 1 to num_phil ->
getarg(2*i+1, max_think_delay[i]); getarg(2*i+2, max_eat_delay[i])
af
var dcap : cap dining_server
write(num_phil, “dining philosophers running”, run_time, “seconds”)
dcap := create dining_server(num_phil)
fa i := 1 to num_phil ->
create philosopher(i, dcap, max_think_delay[i], max_eat_delay[i])
af
nap(1000*run_time); write(“must stop now”); stop
end start
/* ............... Example compile and run(s)
LOVELY PROFESSIONAL UNIVERSITY 105