Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
49
Task/Dining-philosophers/Phix/dining-philosophers.phix
Normal file
49
Task/Dining-philosophers/Phix/dining-philosophers.phix
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
integer fork1 = init_cs(),
|
||||
fork2 = init_cs(),
|
||||
fork3 = init_cs(),
|
||||
fork4 = init_cs(),
|
||||
fork5 = init_cs()
|
||||
integer terminate = 0 -- control flag
|
||||
|
||||
procedure person(sequence name, atom left_fork, atom right_fork)
|
||||
-- (except Russell, who gets left and right the other way round)
|
||||
while terminate=0 do
|
||||
enter_cs(left_fork)
|
||||
enter_cs(right_fork)
|
||||
puts(1, name & " grabs forks.\n")
|
||||
for i=1 to rand(10) do
|
||||
-- if terminate then exit end if
|
||||
puts(1, name & " is eating.\n")
|
||||
-- sleep(1)
|
||||
end for
|
||||
puts(1, name & " puts forks down and leaves the dinning room.\n")
|
||||
leave_cs(left_fork)
|
||||
leave_cs(right_fork)
|
||||
for i=1 to rand(10) do
|
||||
-- if terminate then exit end if
|
||||
puts(1, name & " is thinking.\n")
|
||||
-- sleep(1)
|
||||
end for
|
||||
puts(1, name & " becomes hungry.\n")
|
||||
end while
|
||||
end procedure
|
||||
constant r_person = routine_id("person")
|
||||
|
||||
constant threads = {create_thread(r_person,{"Aristotle",fork1,fork2}),
|
||||
create_thread(r_person,{"Kant",fork2,fork3}),
|
||||
create_thread(r_person,{"Spinoza",fork3,fork4}),
|
||||
create_thread(r_person,{"Marx",fork4,fork5}),
|
||||
-- create_thread(r_person,{"Russell",fork5,fork1})} -- this will deadlock!
|
||||
create_thread(r_person,{"Russell",fork1,fork5})}
|
||||
|
||||
constant ESC = #1B
|
||||
while not find(get_key(),{ESC,'q','Q'}) do
|
||||
sleep(1)
|
||||
end while
|
||||
terminate = 1
|
||||
wait_thread(threads) -- (not strictly necessary)
|
||||
delete_cs(fork1) -- ""
|
||||
delete_cs(fork2)
|
||||
delete_cs(fork3)
|
||||
delete_cs(fork4)
|
||||
delete_cs(fork5)
|
||||
Loading…
Add table
Add a link
Reference in a new issue