synchronization-06 - Synchronization .or: the trickiest bit...

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

View Full Document Right Arrow Icon
Synchronization ..or: the trickiest bit of this course.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Announcements
Background image of page 2
Threads share global memory When a process contains multiple threads, they have Private registers and stack memory (the context switching mechanism needs to save and restore registers when switching from thread to thread) Shared access to the remainder of the process “state”
Background image of page 3

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

View Full DocumentRight Arrow Icon
some slides taken from Mendel Rosenblum's lecture at Stanford Two threads, one counter Popular web server Uses multiple threads to speed things up. Simple shared state error: each thread increments a shared counter to track number of hits What happens when two threads execute concurrently? hits = hits + 1;
Background image of page 4
Shared counters Possible result: lost update! One other possible result: everything works. Difficult to debug Called a “race condition” hits = 0 + 1 read hits (0) hits = 0 + 1 read hits (0) T1 T2 hits = 1 hits = 0 time
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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!”;
Background image of page 7

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

View Full DocumentRight Arrow Icon
Scheduler Assumptions Normally we assume that A scheduler always gives every executable thread opportunities to run In effect, each thread makes finite progress But schedulers aren’t always fair Some threads may get more chances than others To reason about worst case behavior we sometimes think of the scheduler as an adversary trying to “mess up” the algorithm
Background image of page 8
Critical Section Goals Threads do some stuff but eventually might try to access shared data CSEnter(); Critical section CSExit(); T1 T2 time CSEnter(); CSExit(); T1 T2
Background image of page 9

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

View Full DocumentRight Arrow Icon
Critical Section Goals Perhaps they loop (perhaps not!) T1 T2 CSEnter(); Critical section CSExit(); T1 T2 CSEnter(); CSExit();
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
Solving the problem CSEnter() { while(inside) continue; inside = true; } A first idea: Have a boolean flag, inside . Initially false.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 80

synchronization-06 - Synchronization .or: the trickiest bit...

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

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