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

Info icon This 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
Image of page 1

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

View Full Document Right 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?
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern