Download presentation
Presentation is loading. Please wait.
Published byRalf McCoy Modified over 8 years ago
1
(define (make-dining-philosophers n get-forks) (let* ((forks (build-vector n (lambda (i) (make-semaphore 1)))) (philosopher (lambda (i j) (letrec ((f (lambda () (printf "Philosopher ~a is thinking~n" i) (sleep (random 1)) (get-forks i j forks) (printf "Philosopher ~a is eating~n" i) (sleep (random 5)) (semaphore-post (vector-ref forks i)) (semaphore-post (vector-ref forks j)) (f)))) f))) (philosophers (build-list n (lambda (i) (thread (philosopher i (modulo (+ i 1) n))))))) (sleep 30) (display "The philosophers are satiated!") (for-each kill-thread philosophers))) The Dining Philosophers
2
(define (ordinary i j forks) (semaphore-wait (vector-ref forks i)) (semaphore-wait (vector-ref forks j))) (define (deadlock i j forks) (semaphore-wait (vector-ref forks i)) (sleep.5) (semaphore-wait (vector-ref forks j))) (define (starvation i j forks) (semaphore-wait (vector-ref forks i)) (unless (semaphore-try-wait? (vector-ref forks j)) (semaphore-post (vector-ref forks i)) (sleep 2) (starvation i j forks))) Get Fork Methods
3
==> (make-dining-philosophers 3 ordinary) Philosopher 2 is thinking Philosopher 1 is thinking Philosopher 0 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 2 is eating The philosophers are satiated!
4
(define (deadlock i j forks) (semaphore-wait (vector-ref forks i)) (sleep.5) (semaphore-wait (vector-ref forks j))) Deadlock
5
==> (make-dining-philosophers 3 deadlock) Philosopher 2 is thinking Philosopher 1 is thinking Philosopher 0 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 1 is eating Philosopher 1 is thinking deadlock!
6
(define (starvation i j forks) (semaphore-wait (vector-ref forks i)) (unless (semaphore-try-wait? (vector-ref forks j)) (semaphore-post (vector-ref forks i)) (sleep 2) (starvation i j forks))) Starvation
7
==> (make-dining-philosophers 4 starvation) Philosopher 3 is thinking Philosopher 2 is thinking Philosopher 1 is thinking Philosopher 0 is thinking Philosopher 3 is eating Philosopher 1 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 3 is thinking Philosopher 3 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 1 is thinking Philosopher 1 is eating Philosopher 3 is thinking Philosopher 1 is thinking Philosopher 2 is eating Philosopher 0 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 2 is thinking Philosopher 2 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 2 is thinking Philosopher 2 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 0 is eating Philosopher 0 is thinking Philosopher 0 is eating The philosophers are satiated!
8
(define careful-/ (lambda (n d) (with-handlers ((exn:application:divide-by-zero? (lambda (exn) 999))) (/ n d)))) ==> (/ 2 3) 2/3 ==> (/ 1 0) /: division by zero ==> (careful-/ 2 3) 2/3 ==> (careful-/ 1 0) 999 ==> Exceptions
9
(define (foo n) (with-handlers ((even? (lambda (x) (/ x 2))) (odd? (lambda (x) (+ x 1)))) (raise n))) ==> (foo 12) 6 ==> (foo 13) 14 ==>
10
(define (foo n) (with-handlers ((even? (lambda (x) (/ x 2))) (odd? (lambda (x) (+ x 1)))) (bar n))) (define (bar n) (raise n)) ==> (foo 12) 6 ==> (foo 13) 14 ==> (bar 12) uncaught exception: 12 ==>
11
(call-with-current-continuation (lambda (k) expr)) (call/cc (lambda (k) expr)) (let/cc k expr) (call-with-escape-continuation (lambda (k) expr)) (call/ec (lambda (k) expr)) (let/ec k expr) Continuations
12
(define (create name) (let/ec exit (letrec ((f (lambda (n) (printf "~a: going down ~a~n" name n) (if (< n 3) (f (+ n 1)) (let/cc deep (exit deep))) (printf "~a: coming up ~a~n" name n)))) (f 0))))
13
==> (define a (create 'first)) first: going down 0 first: going down 1 first: going down 2 first: going down 3 ==> (define b (create 'second)) second: going down 0 second: going down 1 second: going down 2 second: going down 3 ==> (a 0) first: coming up 3 first: coming up 2 first: coming up 1 first: coming up 0 ==> (b 0) second: coming up 3 second: coming up 2 second: coming up 1 second: coming up 0 ==> (a 0) procedure application: expected procedure, given: # ;... ==> (b 0) procedure application: expected procedure, given: # ;...
14
(define *exit-continuation* 0) (define *continue-continuation* 0) (define (start) (let/ec exit-cont (set! *exit-continuation* exit-cont) (let/ec continue-cont (set! *continue-continuation* continue-cont)) (echo "continuing") (program-loop)) (echo "sayonara")) (define (program-loop) (if (= (random 10) 0) (*exit-continuation* 0) (*continue-continuation* 0)))
15
==> (start) continuing sayonara ==> (start) continuing sayonara ==>
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.