05-synchronization - Synchronization Prof Sirer and Van...

Info icon This preview shows pages 1–14. Sign up to view the full content.

Synchronization Prof. Sirer and Van Renesse CS 4410 Cornell University
Image of page 1

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

Threads share global memory When a process contains multiple threads, they have Private registers and stack memory (the context switching mechanism saves and restores registers when switching from thread to thread) Shared access to the remainder of the process “state”
Image of page 2
Two threads, one variable Two threads updating a single shared variable One thread wants to decrement amount by $10K The other thread wants to decrement amount by 50% What happens when two threads execute concurrently? amount= amount - 10000; amount = 0.50 * amount amount= 100000;
Image of page 3

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

Two threads, one variable r1 = load from amount r1 = r1 - 10000; store r1 to amount r1 = load from amoun r1 = 0.5 * r1 store r1 to amount amount= 100000; amount= ?
Image of page 4
Two threads, one variable r1 = load from amount r1 = r1 - 10000; store r1 to amount r1 = load from amoun r1 = 0.5 * r1 store r1 to amount amount= 100000; amount= ?
Image of page 5

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

Two threads, one variable r1 = load from amount r1 = r1 - 10000; store r1 to amount r1 = load from amoun r1 = 0.5 * r1 store r1 to amount amount= 100000; amount= ?
Image of page 6
Shared counters One possible result: everything works! Another possible result: lost update! Difficult to debug Called a “race condition”
Image of page 7

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

Race conditions Def: a timing dependent error involving shared state Whether it happens depends on how threads scheduled In effect, once thread A starts doing something, it needs to “race” to finish it because if thread B looks at the shared memory region before A is done, A’s change will be lost. Hard to detect: All possible schedules have to be safe Number of possible schedule permutations is huge Some bad schedules? Some that will work sometimes? they are intermittent Timing dependent = small changes can hide bug
Image of page 8
If i is shared, and initialized to 0 Who wins? Is it guaranteed that someone wins? What if both threads run on identical speed CPU executing in parallel Scheduler assumptions Process b: while(i > -10) i = i - 1; print “B won!”; Process a: while(i < 10) i = i + 1; print “A won!”;
Image of page 9

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

Critical Section Goals Threads do some stuff but eventually might try to access shared data CSEnter(); Critical section CSExit(); T1 T2 time CSEnter(); Critical section CSExit(); T1 T2
Image of page 10
Critical Section Goals Perhaps they loop (perhaps not!) T1 T2 CSEnter(); Critical section CSExit(); T1 T2 CSEnter(); Critical section CSExit();
Image of page 11

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

Critical Section Goals We would like Safety: No more than one thread can be in a critical section at any time. Liveness: A thread that is seeking to enter the critical section will eventually succeed Fairness: If two threads are both trying to enter a critical section, they have equal chances of success … in practice, fairness is rarely guaranteed
Image of page 12
Too much milk problem Two roommates want to ensure that the fridge is always stocked with milk If the fridge is empty, they need to restock it But they don’t want to buy too much milk Caveats They can only communicate by reading and writing onto a notepad on the fridge
Image of page 13

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

Image of page 14
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