CS162 Operating Systems and Systems Programming Lecture 4 Synchronization Atomic operations Locks Semaphores January 31 2011 Ion Stoica http inst eecs berkeley edu cs162 Space Shuttle Example Original Space Shuttle launch aborted 20 minutes before scheduled launch Shuttle has five computers Four run the Primary Avionics Software System PASS PASS Asynchronous and real time Runs all of the control systems Results synchronized and compared every 3 to 4 ms BFS The Fifth computer is the Backup Flight System BFS stays synchronized in case it is needed Written by completely different team than PASS Countdown aborted because BFS disagreed with PASS A 1 67 chance that PASS was out of sync one cycle Bug due to modifications in initialization code of PASS A delayed init request placed into timer queue As a result timer queue not empty at expected time to force use of hardware clock Bug not found during extensive simulation 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 2 Another Concurrent Program Example Two threads A and B compete with each other One tries to increment a shared counter The other tries to decrement the counter Thread A i 0 while i 10 i i 1 printf A wins Thread B i 0 while i 10 i i 1 printf B wins Assume that memory loads and stores are atomic but incrementing and decrementing are not atomic Who wins Is it guaranteed that someone wins Why or why not What it both threads have their own CPU running at same speed Is it guaranteed that it goes on forever 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 3 Goals for Today Synchronization Hardware Support for Synchronization Higher level Synchronization Abstractions Semaphores monitors and condition variables Programming paradigms for concurrent programs Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Gagne Many slides generated by Kubiatowicz 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 4 Motivation Too much milk Great thing about OS s analogy between problems in OS and problems in real life Help you understand real life problems better But computers are much stupider than people Example People need to coordinate Time 3 00 3 05 3 10 3 15 3 20 3 25 3 30 31 1 11 Person A Look in Fridge Out of milk Leave for store Arrive at store Buy milk Arrive home put milk away Person B Look in Fridge Out of milk Leave for store Arrive at store Buy milk Arrive home put milk away Ion Stoica CS162 UCB Spring 2011 Lec 4 5 Definitions Synchronization using atomic operations to ensure cooperation between threads For now only loads and stores are atomic We ll show its hard to build anything useful with only reads and writes Mutual Exclusion ensuring that only one thread does a particular thing at a time One thread excludes the other while doing its task Critical Section piece of code that only one thread can execute at once 31 1 11 Critical section is the result of mutual exclusion Critical section and mutual exclusion are two ways of describing the same thing Ion Stoica CS162 UCB Spring 2011 Lec 4 6 More Definitions Lock prevents someone from doing something Lock before entering critical section and before accessing shared data Unlock when leaving after accessing shared data Wait if locked Important idea all synchronization involves waiting For example fix the milk problem by putting a key on the refrigerator Lock it and take key if you are going to go buy milk Fixes too much roommate angry if only wants orange juice know how to make a lock yet Of Course We don t Ion Stoica CS162 UCB Spring 2011 31 1 11 Lec 4 7 Too Much Milk Correctness Properties Need to be careful about correctness of concurrent programs since non deterministic Always write down behavior first Impulse is to start coding first then when it doesn t work pull hair out Instead think first then code What are the correctness properties for the Too much milk problem Never more than one person buys Someone buys if needed Restrict ourselves to use only atomic load and store operations as building blocks 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 8 Too Much Milk Solution 1 Use a note to avoid buying too much milk Leave a note before buying kind of lock Remove note after buying kind of unlock Don t buy if note wait Suppose a computer tries this remember only memory read write are atomic if noMilk if noNote leave Note buy milk remove note Result 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 9 Too Much Milk Solution 1 Still too much milk but only occasionally Thread A if noMilk if noNote Thread B if noMilk if noNote leave Note buy milk remove note leave Note buy milk Thread can get context switched after checking milk and note but before buying milk Solution makes problem worse since fails intermittently Makes it really hard to debug Must work despite what the thread dispatcher does 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 10 Too Much Milk Solution 1 Clearly the Note is not quite blocking enough Let s try to fix this by placing note first Another try at previous solution leave Note if noMilk if noNote buy milk remove note What happens here Well with human probably nothing bad With computer no one ever buys milk 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 11 Too Much Milk Solution 2 How about labeled notes Now we can leave note before checking Algorithm looks like this Thread A leave note A if noNote B if noMilk buy Milk remove note A Thread B leave note B if noNote A if noMilk buy Milk remove note B Does this work 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 12 Too Much Milk Solution 2 Possible for neither thread to buy milk Thread A leave note A Thread B leave note B if noNote A if noMilk buy Milk if noNote B if noMilk buy Milk Really insidious remove note B Unlikely that this would happen but will at worse possible time 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 13 Too Much Milk Solution 2 problem I m not getting milk You re getting milk This kind of lockup is called starvation 31 1 11 Ion Stoica CS162 UCB Spring 2011 Lec 4 14 Review Too Much Milk Solution 3 Here is a possible two note solution Thread A Thread B leave note A while note B X do nothing buy milk if noMilk buy milk remove note B remove note A leave note B if noNote A Y if noMilk Does this work Yes Both can guarantee that It is safe to buy or Other will buy ok to quit At X if no note B safe for A to buy otherwise wait to find out what will happen At Y if no note A safe for B to buy …
View Full Document
Unlocking...