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
|
|
@ -0,0 +1,39 @@
|
|||
(lib 'tasks)
|
||||
|
||||
(define names #(Aristotle Kant Spinoza Marx Russell))
|
||||
(define abouts #("Wittgenstein" "the nature of the World" "Kant" "starving"
|
||||
"spaghettis" "the essence of things" "Ω" "📞" "⚽️" "🍅" "🌿"
|
||||
"philosophy" "💔" "👠" "rosetta code" "his to-do list" ))
|
||||
(define (about) (format "thinking about %a." (vector-ref abouts (random (vector-length abouts)))))
|
||||
|
||||
;; statistics
|
||||
(define rounds (make-vector 5 0))
|
||||
(define (eat i) (vector-set! rounds i (1+ (vector-ref rounds i))))
|
||||
|
||||
;; forks are resources = semaphores
|
||||
(define (left i) i)
|
||||
(define (right i) (modulo (1+ i) 5))
|
||||
(define forks (for/vector ((i 5)) (make-semaphore 1)))
|
||||
(define (fork i) (vector-ref forks i))
|
||||
|
||||
(define laquais (make-semaphore 4))
|
||||
|
||||
;; philosophers tasks
|
||||
(define (philo i)
|
||||
;; thinking
|
||||
(writeln (vector-ref names i) (about))
|
||||
(sleep (+ 2000 (random 1000)))
|
||||
(wait laquais)
|
||||
;; get forks
|
||||
(writeln (vector-ref names i) 'sitting)
|
||||
(wait (fork (left i)))
|
||||
(wait (fork (right i)))
|
||||
(writeln (vector-ref names i) 'eating)
|
||||
(eat i)
|
||||
(sleep (+ 6000 (random 1000)))
|
||||
;; put-forks
|
||||
(signal (fork (left i)))
|
||||
(signal (fork (right i)))
|
||||
(signal laquais)
|
||||
i)
|
||||
(define tasks (for/vector ((i 5)) (make-task philo i)))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(define (observe dummmy)
|
||||
(writeln 'observer 'rounds= rounds)
|
||||
#t)
|
||||
(define observer (make-task observe #t ))
|
||||
|
||||
(define (dinner)
|
||||
(task-run observer 5000)
|
||||
(for ((t tasks)) (task-run t)))
|
||||
|
||||
(dinner)
|
||||
41
Task/Dining-philosophers/Nim/dining-philosophers.nim
Normal file
41
Task/Dining-philosophers/Nim/dining-philosophers.nim
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import threadpool, locks, math, os
|
||||
randomize()
|
||||
|
||||
type Philosopher = ref object
|
||||
name: string
|
||||
forkLeft, forkRight: int
|
||||
|
||||
const
|
||||
n = 5
|
||||
names = ["Aristotle", "Kant", "Spinoza", "Marx", "Russell"]
|
||||
|
||||
var
|
||||
forks: array[n, TLock]
|
||||
phils: array[n, Philosopher]
|
||||
threads: array[n, TThread[Philosopher]]
|
||||
|
||||
proc run(p: Philosopher) {.thread.} =
|
||||
sleep random(1 .. 10) * 500
|
||||
echo p.name, " is hungry."
|
||||
|
||||
acquire forks[min(p.forkLeft, p.forkRight)]
|
||||
sleep random(1 .. 5) * 500
|
||||
acquire forks[max(p.forkLeft, p.forkRight)]
|
||||
|
||||
echo p.name, " starts eating."
|
||||
sleep random(1 .. 10) * 500
|
||||
|
||||
echo p.name, " finishes eating and leaves to think."
|
||||
|
||||
release forks[p.forkLeft]
|
||||
release forks[p.forkRight]
|
||||
|
||||
for i in 0 .. <n:
|
||||
initLock forks[i]
|
||||
phils[i] = Philosopher(
|
||||
name: names[i],
|
||||
forkLeft: i,
|
||||
forkRight: (i+1) mod n)
|
||||
threads[i].createThread run, phils[i]
|
||||
|
||||
threads.joinThreads
|
||||
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