DOC PREVIEW
Berkeley COMPSCI 61A - Lecture Notes

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61A Lecture 21Clicker poll Dining PhilosophersSlide 4Problems with ConcurrencyBELOW the line…What if these “happened” one after the otherIf these execute in parallel: Possibility for Incorrect Results!How many possible outcomes?EVERY ordering!If these execute in parallel… Write out the sequence of eventsSlide 12Possible outcomesOur definition of a “correct answer”?Protecting from incorrectnessSerializers protect things And make things they protect serial (def: taking place in a series)Slide 17Slide 18Slide 19We’ve seen INCORRECT… now INEFFICIENCYYou’ve seen INCORRECT and INEFFICIENT… now DEADLOCKSlide 22Slide 23Implementing SerializersWrinkles and I want to shareWrite make-mutex(make-mutex)SolutionPowerPoint PresentationSlide 29clear!Write test-and-set!test-and-set! SOLUTIONSlide 33“Correct answers”CS61A Lecture 212011-07-25Colleen Lewis1Clicker poll  Are you allowed to talk about the midterm on piazza or in public yet?A)YesB)No2Dining PhilosophersPhilosophers are sitting around a large round table, each with a bowl of Chinese food in front of him/her. Between periods of deep thought they may start eating whenever they want to, with their bowls being filled frequently. But there are only 5 chopsticks available, one to the left of each bowl. When a philosopher wants to start eating, he/she must pick up the chopstick to the left of his bowl and the chopstick to the right of his bowl. 3Dining PhilosophersA) Heard of it before (and know the point)B) Never heard of it before (but get it)C) Never heard of it before (but going to get it)4Problems with Concurrency•Incorrectness•Inefficiency•Deadlock•Unfairness5BELOW the line…(set! x (+ 1 x))•Lookup x•Add 1 to x•Set x6Actually one set! is composed of 3 stepsWhat if these “happened” one after the other(set! x (+ 1 x)) (set! x (+ 1 x))•P1: Lookup x•P1: Add 1 to x•P1: Set x7•P2: Lookup x•P2: Add 1 to x•P2: Set xX: 100If these execute in parallel:Possibility for Incorrect Results! (set! x (+ 1 x)) (set! x (+ 1 x))•P1: Lookup x•P1: Add 1 to x•P1: Set x8•P2: Lookup x•P2: Add 1 to x•P2: Set xX: 100 Within a process they are NEVER re-ordered!Only interleaved with another processHow many possible outcomes?(define x 100)(parallel-execute (lambda() (set! x (+ x 6)))(lambda() (set! x (+ x 5))))A)1 possible outcomeB)2 possible outcomesC)3 possible outcomesD)4 possible outcomesE)5 possible outcomes9Thunk: Lambda of with no argumentsEVERY ordering!•1 1 2 2•1 2 1 2•2 1 1 2•1 2 2 1•2 1 2 1•2 2 1 110P1 then P2P2 then P1P2 clobbers P1P1 clobbers P2If these execute in parallel… Write out the sequence of events(define x 10)(set! x (+ x x)) (set! x (+ 1 x))•P1: Lookup x•P1: Lookup x•P1: Add x to x•P1: Set x11•P2: Lookup x•P2: Add 1 to x•P2: Set xA) 2 possible outcomes for xB) 3 possible outcomes for xC) 4 possible outcomes for xD) 5 possible outcomes for xE) More than 5Critical sectionEVERY ordering!•1 1 1 2 2•1 1 2 1 2•1 2 1 1 2•2 1 1 1 2•1 1 2 2 1•1 2 1 2 1•2 1 1 2 1•1 2 2 1 1•2 1 2 1 1•2 2 1 1 1 12P1 then P2P2 then P1P2 clobbers P1P1 clobbers P2P2 between P1’s lookupsPossible outcomes(set! x (+ x x)) (set! x (+ 1 x))P1 then P2•P1: Lookup x•P1: Lookup x•P1: Set x•P2: Lookup x•P2: Set x13P2 then P1•P2: Lookup x•P2: Set x •P1: Lookup x•P1: Lookup x•P1: Set xP2 between P1’s lookups•P1: Lookup x•P2: Lookup x•P2: Set x •P1: Lookup x•P1: Set xP2 clobbers P1•P1: Lookup x•P1: Lookup x•P2: Lookup x•P1: Set x•P2: Set x P1 clobbers P2•P1: Lookup x•P1: Lookup x•P2: Lookup x•P2: Set x •P1: Set xOur definition of a “correct answer”?(define x 10)(parallel-execute (lambda() (set! x (+ x x)))(lambda() (set! x (+ 1 x))))Correct answers:21 (line 1 first)22 (line 2 first)14“Ensure that a concurrent system produces the same result as if the processes had run sequentially in some order.”Protecting from incorrectness15Serializers protect thingsAnd make things they protect serial(def: taking place in a series)(define stephanie (make-serializer))(define phill (make-serializer))(define hamilton (make-serializer))16Occupied. You’ve got to wait!Serializers protect thingsAnd make things they protect serial(def: taking place in a series)(define stephanie-x (make-serializer))(parallel-execute (stephanie-x (lambda()(set! x (+ x 1))) (stephanie-x (lambda()(set! x (+ x x))) (stephanie-x (lambda()(set! x (+ x 9))))Serializer stephanie-x will make sure nothing she protects happen concurrently. 17“Ensure that a concurrent system produces the same result as if the processes had run sequentially in some order.”Serializers protect thingsAnd make things they protect serial(def: taking place in a series)(define hamilton-x (make-serializer))(define phill-y (make-serializer))(parallel-execute (hamilton-x (lambda()(set! x (+ x 1))) (hamilton-x (lambda()(set! x (+ x x))) (phill-y (lambda()(set! y (+ y 1))) (phill-y (lambda()(set! y (+ y y))) (hamilton-x (lambda()(set! x (+ x 9))))18Serializers protect thingsAnd make things they protect serial(def: taking place in a series)(define x 10)(define stephanie-x (make-serializer))(define hamilton-x (make-serializer))(parallel-execute (stephanie-x (lambda()(set! x (+ x 1))) (hamilton-x (lambda()(set! x (+ x x)))))Will this ensure the answer will be 21 or 22?A. Yes B. No C. Not sure19We’ve seen INCORRECT… now INEFFICIENCY20(define phill-xy (make-serializer))(parallel-execute (phill-xy (lambda()(set! x (+ x 1))) (phill-xy (lambda()(set! x (+ x x))) (phill-xy (lambda()(set! y (+ y 1))) (phill-xy (lambda()(set! y (+ y y))) (phill-xy (lambda()(set! x (+ x 9))))It would be correct to interleave x’s and y’sYou’ve seen INCORRECT and INEFFICIENT… now DEADLOCK(define serial-x (make-serializer))(define serial-y (make-serializer))(parallel-execute (serial-x (lambda()(set! x (+ x 1))) (serial-y (lambda()(set! y (+ y y)(set! y (+ y 1))) (serial-y (serial-x (lambda () (set! y (+ y 1))(set! x (+ x 1))))))21Critical sectionYou’ve seen INCORRECT and INEFFICIENT… now DEADLOCK(define serial-x (make-serializer))(define serial-y (make-serializer))(parallel-execute (serial-y (serial-x (lambda () (set! y (+ y 1))(set! x (+ x 1))))) (serial-x (serial-y (lambda () (set! y (+ y 1))(set! x (+ x 1))))))22Problems with Concurrency•Incorrectness•Inefficiency•Deadlock•Unfairness23Implementing SerializersWith a mutex24Wrinkles and I


View Full Document

Berkeley COMPSCI 61A - Lecture Notes

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?