CS-206 Concurrency Lecture 4 Mutual Exclusion - PARSA
Transcription
CS-206 Concurrency Lecture 4 Mutual Exclusion - PARSA
CS-206 Concurrency Lecture 4 Mutual Exclusion A B cola Spring 2015 Prof. Babak Falsafi parsa.epfl.ch/courses/cs206/ Adapted from slides originally developed by Maurice Herlihy and Nir Shavit from the Art of Multiprocessor Programming, and Babak Falsafi EPFL Copyright 2015 EPFL CS-206 – Spring 2015 Lec.4 - 1 Where are We? M T Lecture & Lab W 16-Feb 17-Feb 18-Feb 19-Feb 20-Feb 23-Feb 24-Feb 25-Feb 26-Feb 27-Feb 2-Mar 3-Mar 4-Mar 5-Mar 9-Mar 10-Mar 11-Mar 12-Mar 13-Mar 16-Mar 17-Mar 18-Mar 19-Mar 20-Mar 23-Mar 24-Mar 25-Mar 26-Mar 27-Mar 30-Mar 31-Mar 1-Apr 2-Apr 3-Apr 6-Apr 7-Apr 8-Apr 9-Apr 10-Apr 13-Apr 14-Apr 15-Apr 16-Apr 17-Apr 20-Apr 21-Apr 22-Apr 23-Apr 24-Apr 27-Apr 28-Apr 29-Apr 30-Apr 1-May 4-May 5-May 6-May 7-May 8-May 11-May 12-May 13-May 14-May 15-May 18-May 19-May 20-May 21-May 22-May 25-May 26-May 27-May 28-May 29-May EPFL CS-206 – Spring 2015 T F 6-Mar u Mutual Exclusion in practice w How do we share? w How do we share well? w Protocols w Properties u Next week w Mutual Exclusion 2 w Formal definiton Lec.4 - 2 Recall: Counter in Shared Memory code Local variables temp = value; value = temp + 1; cache cache Bus 1 cache Bus shared memory Need Mutual Exclusion EPFL CS-206 – Spring 2015 Lec.4 - 3 Sharing requires coordination u Illustrated u Our through a fable Fable w Alice & Bob have pets: a dog and a cat w The pets like to play in a pond w But, they don’t get a long w So, need a protocol to share the pond w Agree on guidelines as to how to share w The protocol would guarantee mutual exclusion EPFL CS-206 – Spring 2015 Lec.4 - 4 Mutual Exclusion, or “Alice & Bob share a pond” A EPFL CS-206 – Spring 2015 B Lec.4 - 5 Alice has a pet A EPFL CS-206 – Spring 2015 B Lec.4 - 6 Bob has a pet A EPFL CS-206 – Spring 2015 B Lec.4 - 7 The Problem A B The pets don’t get along EPFL CS-206 – Spring 2015 Lec.4 - 8 Formalizing the Problem u Two types of formal properties in asynchronous computation: u Safety Properties w Nothing u Liveness bad happens ever Properties w Something EPFL CS-206 – Spring 2015 good happens eventually Lec.4 - 9 Formalizing our Problem u Mutual Exclusion w Both pets never in pond simultaneously w This is a safety property u No Deadlock w if only one wants in, it gets in w if both want in, one gets in w This is a liveness property EPFL CS-206 – Spring 2015 Lec.4 - 10 Simple Protocol u Idea w Just look at the pond u Gotcha (means “potential problems”) w Not atomic w Trees obscure the view EPFL CS-206 – Spring 2015 Lec.4 - 11 Interpretation u Threads can’t “see” what other threads are doing u Explicit communication required for coordination EPFL CS-206 – Spring 2015 Lec.4 - 12 Cell Phone Protocol u Idea w Alice calls Bob (or vice-versa) u Gotcha w Bob takes shower w Alice may need to recharge battery w Bob out of cell phone tower reach… EPFL CS-206 – Spring 2015 Lec.4 - 13 Interpretation u Message-passing doesn’t work u Recipient might not be w Listening w There at all u Communication must be w Persistent (like writing) w Not transient (like speaking) EPFL CS-206 – Spring 2015 Lec.4 - 14 Alice comes up with a clever solution u Beer/Cola cans! w Used to send a signal w Sort of like conveying a “ready” bit w Fallen can = ready, upright can = not ready u One or more beer cans by Alice’s window (on windowsill) u Each with string running to Bob’s house u To send a signal, Bob pulls string u …and knock the can over EPFL CS-206 – Spring 2015 Lec.4 - 15 EPFL CS-206 – Spring 2015 cola cola Can Protocol Lec.4 - 16 Bob conveys a bit B cola A EPFL CS-206 – Spring 2015 Lec.4 - 17 Bob conveys a bit A B cola EPFL CS-206 – Spring 2015 Lec.4 - 18 Can Protocol u Idea w Cans by Alice’s window w Strings lead to Bob’s house w Bob pulls strings, knocks over cans u Gotcha w Cans cannot be reused w Bob runs out of cans w What if Alice takes a long time to reset (goes on vacation)? EPFL CS-206 – Spring 2015 Lec.4 - 19 Interpretation u Sending a signal (interrupt to set a bit) w Does not solve mutual exclusion w Sender sets fixed bit in receiver’s space w Receiver resets bit when ready w Requires unbounded number of interrupt bits EPFL CS-206 – Spring 2015 Lec.4 - 20 Flag Protocol A EPFL CS-206 – Spring 2015 B Lec.4 - 21 Alice’s Protocol (sort of) A EPFL CS-206 – Spring 2015 B Lec.4 - 22 Bob’s Protocol (sort of) A EPFL CS-206 – Spring 2015 B Lec.4 - 23 Alice’s Protocol u Raise flag u Wait until Bob’s flag is down u Unleash pet u Lower flag when pet returns EPFL CS-206 – Spring 2015 Lec.4 - 24 Bob’s Protocol u Raise flag u Wait until Alice’s flag is down u Unleash pet u Lower flag when pet returns EPFL CS-206 – Spring 2015 Lec.4 - 25 Bob’s Protocol (2nd try) u Raise flag u While Alice’s flag is up w Lower flag w Wait for Alice’s flag to go down w Raise flag u Unleash pet u Lower flag when pet returns EPFL CS-206 – Spring 2015 Lec.4 - 26 Bob’s Protocol u Raise flag u While Alice’s flag is up Bob defers to Alice w Lower flag w Wait for Alice’s flag to go down w Raise flag u Unleash pet u Lower flag when pet returns EPFL CS-206 – Spring 2015 Lec.4 - 27 The Flag Principle u Raise the flag u Look at other’s flag u Flag Principle: w If each raises and looks, then w Last to look must see both flags up EPFL CS-206 – Spring 2015 Lec.4 - 28 Proof of Mutual Exclusion u Assume both pets in pond w Derive a contradiction w By reasoning backwards u Consider the last time Alice and Bob each looked before letting the pets in u Without loss of generality assume Alice was the last to look… EPFL CS-206 – Spring 2015 Lec.4 - 29 Proof Bob last raised flag Alice last raised her flag Alice’s last look Bob’s last look time Alice must have seen Bob’s Flag. A Contradiction EPFL CS-206 – Spring 2015 Lec.4 - 30 Proof of No Deadlock u If only one pet wants in, it gets in u Deadlock requires both continually trying to get in u If Bob sees Alice’s flag, he gives her priority (a gentleman…) EPFL CS-206 – Spring 2015 Lec.4 - 31 Remarks u Protocol w Bob’s u Protocol w If is unfair pet might never get in uses waiting Bob is eaten by his pet, Alice’s pet might never get in EPFL CS-206 – Spring 2015 Lec.4 - 32 Moral of Story u Mutual Exclusion cannot be solved by w transient communication (cell phones) w interrupts (cans) u It can be solved by w one-bit shared variables w that can be read or written EPFL CS-206 – Spring 2015 Lec.4 - 33 The Fable Continues u Alice and Bob fall in love & marry u Then they fall out of love & divorce w She gets the pets w He has to feed them u Leading to a new coordination problem: w Producer-Consumer w Used everywhere in parallel computing/distributed systems w E.g., network buffers (shared between processor & network) w E.g., OS scheduler (queues holding active threads) EPFL CS-206 – Spring 2015 Lec.4 - 34 Bob Puts Food in the Pond A EPFL CS-206 – Spring 2015 Lec.4 - 35 Alice releases her pets to Feed mmm… mmm… EPFL CS-206 – Spring 2015 B Lec.4 - 36 Producer/Consumer u Alice and Bob can’t meet w Each has restraining order (by court) on other w So he puts food in the pond w And later, she releases the pets u Avoid w Releasing pets when there’s no food w Putting out food if uneaten food remains EPFL CS-206 – Spring 2015 Lec.4 - 37 Producer/Consumer u Need a mechanism so that w Bob lets Alice know when food has been put out w Alice lets Bob know when to put out more food EPFL CS-206 – Spring 2015 Lec.4 - 38 Surprise Solution B cola A EPFL CS-206 – Spring 2015 Lec.4 - 39 Bob puts food in Pond B cola A EPFL CS-206 – Spring 2015 Lec.4 - 40 Bob knocks over Can A B cola EPFL CS-206 – Spring 2015 Lec.4 - 41 Alice Releases Pets A yum… yum… B cola EPFL CS-206 – Spring 2015 Lec.4 - 42 Alice Resets Can when Pets are Fed B cola A EPFL CS-206 – Spring 2015 Lec.4 - 43 Pseudocode while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset(); } Alice’s code EPFL CS-206 – Spring 2015 Lec.4 - 44 Pseudocode while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset(); while (true) { } while (can.isDown()){}; Bob’s code pond.stockWithFood(); can.knockOver(); Alice’s code EPFL CS-206 – Spring 2015 } Lec.4 - 45 Correctness u Mutual w Pets u No Exclusion and Bob never together in pond Starvation if Bob always willing to feed, and pets always famished, then pets eat infinitely often EPFL CS-206 – Spring 2015 Lec.4 - 46 Correctness u Mutual w Pets u No Exclusion safety and Bob never together in pond Starvation liveness if Bob always willing to feed, and pets always famished, then pets eat infinitely often. u Producer/Consumer safety The pets never enter pond unless there is food, and Bob never provides food if there is unconsumed food. EPFL CS-206 – Spring 2015 Lec.4 - 47 Could Also Solve Using Flags A EPFL CS-206 – Spring 2015 B Lec.4 - 48 Waiting u Both solutions use waiting w while(mumble){} u In some cases waiting is problematic w If one participant is delayed w So is everyone else w But delays are common & unpredictable EPFL CS-206 – Spring 2015 Lec.4 - 49 The Fable drags on … u Bob and Alice still have issues u So they need to communicate u They agree to use billboards … EPFL CS-206 – Spring 2015 Lec.4 - 50 Readers/Writers u Alice & Bob agree to write messages on a billboard u Alice starts writing one letter at a time w First she writes “wash the car” w Then writes “sell the house” w What does Bob read? EPFL CS-206 – Spring 2015 Lec.4 - 51 Billboards are Large B D A C E 2 3 1 EPFL CS-206 – Spring 2015 3 1 Letter Tiles From Scrabble™ box Lec.4 - 52 Write One Letter at a Time … W A S 4 1 1 H 4 B D A C E 2 3 1 EPFL CS-206 – Spring 2015 3 1 Lec.4 - 53 To post a message W A S H T H E C A R 4 1 1 4 1 4 1 3 1 1 whew EPFL CS-206 – Spring 2015 Lec.4 - 54 Let’s send another message S E L L 1 1 1 1 L A V A 1 1 4 1 L A M PS 1 1 3 EPFL CS-206 – Spring 2015 3 1 Lec.4 - 55 Uh-Oh S E L L 1 1 1 1 T H E 1 4 1 C A R 3 1 1 L 1 OK EPFL CS-206 – Spring 2015 Lec.4 - 56 Readers/Writers u Devise a protocol so that w Writer writes one letter at a time w Reader reads one letter at a time w Reader sees “snapshot” w Old message or new message w No mixed messages EPFL CS-206 – Spring 2015 Lec.4 - 57 Readers/Writers (continued) u Easy with mutual exclusion u But mutual exclusion requires waiting w One waits for the other w Everyone executes sequentially u Remarkably w We can solve R/W without mutual exclusion EPFL CS-206 – Spring 2015 Lec.4 - 58 Why do we care? u We want as much of the code as possible to execute concurrently (in parallel) u A larger sequential part implies reduced performance u Amdahl’s law: this relation is not linear… EPFL CS-206 – Spring 2015 Lec.4 - 59 Parallelizing Readers/Writers u Use two billboards w While Bob reads from one… w Alice writes to the other u How do they know where to read/write? w Third billboard w Tells Bob which board to read EPFL CS-206 – Spring 2015 Lec.4 - 60 Wait-free protocol S1 E1 L1 L1 T1 H4 2 W4 A1 S1 H4 T1 H4 E1 C3 A1 R1 H4 H H H4444 W..a..s..h… EPFL CS-206 – Spring 2015 Lec.4 - 61 Wait-free protocol S1 E1 L1 L1 T1 H4 E1 2 D2 O4 G3 W4 A1 S1 H4 T1 H4 E1 C3 A1 R1 1 Wash the car! Got it! EPFL CS-206 – Spring 2015 Lec.4 - 62 Wait-free protocol S1 E1 L1 L1 T1 H4 E1 1 D2 O4 G3 W4 A1 S1 H4 T1 H4 E1 C3 A1 R1 Yay, new message on board 1! EPFL CS-206 – Spring 2015 Lec.4 - 63 Wait-free protocol S1 E1 L1 L1 T1 H4 E1 1 D2 O4 G3 M4 O4 V4 E4 Sell the dog? Ok! EPFL CS-206 – Spring 2015 A4 H4 E1 N4 C3 A1 R1 Lec.4 - 64 Wait-free protocol u Is this protocol entirely wait-free? u Is the protocol correct? u How do you fix it? EPFL CS-206 – Spring 2015 Lec.4 - 65 Summary u Need protocols to coordinate u Mutual exclusion properties w Safety w Liveness w Fairness u Producer/Consumer sharing u Reducing sequential execution/waiting EPFL CS-206 – Spring 2015 Lec.4 - 66