oldweek6a_sol

oldweek6a_sol - CS61A Notes Week 06a: Concurrency and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS61A Notes – Week 06a: Concurrency and Streams (solutions) Concurrency: The Series 1. Here is an attempt to simulate this behavior: (define (eat-talk i)    (define (loop)       (cond ((can-eat? i)              (take-chopsticks i)              (eat-a-while)                    (release-chopsticks i))                   (else (spew-nonsense)))             (loop)    (loop)) (parallel-execute (lambda () (eat-talk 0)) (lambda () (eat-talk 1)) (lambda () (eat-talk 2))) ;; a list of chopstick status, #t if usable, #f if taken (define chopsticks ‘(#t #t #t)) ;; does person i have both chopsticks? (define (can-eat? i)          (and (list-ref chopsticks (right-chopstick i))   (list-ref chopsticks (left-chopstick i)))) ;; let person i take both chopsticks ;; assume (list-set! ls i val) destructively sets the ith element of ;; ls to val (define (take-chopsticks i)    (list-set! chopsticks (right-chopstick i) #f)    (list-set! chopsticks (left-chopstick i) #f)) ;; let person i release both chopsticks (define (release-chopsticks i)    (list-set! chopsticks (right-chopstick i) #t)    (list-set! chopsticks (left-chopstick i) #t)) ;; some helper procedures (define (left-chopstick i) (if (= i 2) 0 (+ i 1))) (define (right-chopstick i) i) Is this correct? If not, what kind of hazard does this create? Incorrect; more than one person could be eating at once (all three check they can eat, all  three take chopsticks, and all three eat). 2.   Here's a proposed fix: (define protector (make-serializer)) (parallel-execute (protector (lambda () (eat-talk 0))) (protector (lambda () (eat-talk 1))) (protector (lambda () (eat-talk 2)))) Does this work? Unfair. Note that eat-talk generates in infinite loop. The serializer makes sure only one  of the three is executed at once, so once parallel-execute picks one to  execute, it’s  going to keep eating and eating, and the others won’t even get to execute at all. Justin Chen CS61A Spring 2010 – notes courtesy of Chung Wu 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3. Here’s another proposed fix: use one mutex per chopstick, and acquire both before doing anything: (define protectors    (list (make-mutex) (make-mutex) (make-mutex))) (define (eat-talk i)    (define (loop)       ((list-ref protectors (right-chopstick i)) ‘acquire)       ((list-ref protectors (left-chopstick i)) ‘acquire)       (cond . .. ;; as before)       ((list-ref protectors (right-chopstick i)) ‘release)       ((list-ref protectors (left-chopstick i)) ‘release)       (loop))    (loop)) Does that work? Deadlock. Suppose all three grab the chopstick on the left at the same time; then all 
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2011 for the course CS 61A taught by Professor Harvey during the Fall '08 term at Berkeley.

Page1 / 6

oldweek6a_sol - CS61A Notes Week 06a: Concurrency and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online