05-synchronization - Synchronization Prof. Sirer CS 4410...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Synchronization Prof. Sirer CS 4410 Cornell University 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 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; Two threads, one variable r1 = load from amount r1 = r1 - 10000; store r1 to amount r1 = load from amount r1 = 0.5 * r1 store r1 to amount amount= 100000; amount= ? Two threads, one variable r1 = load from amount r1 = r1 - 10000; store r1 to amount r1 = load from amount r1 = 0.5 * r1 store r1 to amount amount= 100000; amount= ? Two threads, one variable r1 = load from amount r1 = r1 - 10000; store r1 to amount r1 = load from amount r1 = 0.5 * r1 store r1 to amount amount= 100000; amount= ? Shared counters One possible result: everything works! Another possible result: lost update! Difficult to debug Called a race condition 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, As 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 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!; 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 Critical Section Goals Perhaps they loop (perhaps not!) T1 T2 CSEnter(); Critical section CSExit(); T1 T2 CSEnter(); Critical section CSExit(); Critical Section Goals...
View Full Document

This note was uploaded on 12/07/2011 for the course CS 4410 taught by Professor Vollset during the Spring '07 term at Cornell University (Engineering School).

Page1 / 78

05-synchronization - Synchronization Prof. Sirer CS 4410...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online