Скачать презентацию Synchronization Part 1 CSE 410 Spring 2008 Computer Скачать презентацию Synchronization Part 1 CSE 410 Spring 2008 Computer

083f43c268ded617ec9540170e2381be.ppt

  • Количество слайдов: 39

Synchronization Part 1 CSE 410, Spring 2008 Computer Systems http: //www. cs. washington. edu/410 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems http: //www. cs. washington. edu/410 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 1

Readings and References • Reading » Chapter 6, Operating System Concepts, Silberschatz, Galvin, and Readings and References • Reading » Chapter 6, Operating System Concepts, Silberschatz, Galvin, and Gagne. Read the following sections: 6. 1, 6. 2, 6. 3 • Other References » Chapter 6, Multithreaded Programming with Pthreads, First edition, Bil Lewis and Daniel J. Berg, Sun Microsystems Press » Sections 5. 8. 3, Atomicity and Atomic Changes, 5. 8. 4, Critical Regions with Interrupts Enabled, See MIPS Run, Dominic Sweetman 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 2

Story of the Oracle • Concurrent data access is complex » The solutions presented Story of the Oracle • Concurrent data access is complex » The solutions presented in this chapter are a great start » Individually each have advantages and disadvantages, and can be used in conjunction • It all happened one day in a microchip garden… 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 3

Too Much Milk 3: 00 3: 05 3: 10 3: 15 3: 20 3: Too Much Milk 3: 00 3: 05 3: 10 3: 15 3: 20 3: 25 3: 30 3/19/2018 You Look in fridge; no milk Leave for store Arrive at store Buy milk Arrive home; put milk away Your Roommate Look in fridge; no milk Leave for store Arrive at store Buy milk Arrive home; put milk away Oh no, too much milk! cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 4

Modeling the Problem • Model you and your roommate as threads • “Looking in Modeling the Problem • Model you and your roommate as threads • “Looking in the fridge” and “putting away milk” are reading/writing a variable YOU: YOUR ROOMMATE: // look in fridge if( milk. Amount == 0 ) { // buy milk. Amount++; } 3/19/2018 // look in fridge if( milk. Amount == 0 ) { // buy milk. Amount++; } cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 5

Correctness Properties • Decomposed into safety and liveness » safety • the program never Correctness Properties • Decomposed into safety and liveness » safety • the program never does anything bad (mutual exclusion) » liveness • the program eventually does something good (progress and bounded waiting) • Although easy to state, these properties are not always easy to meet (or prove!) 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 6

Synchronization Definitions • Synchronization » coordinated access by more than one thread to shared Synchronization Definitions • Synchronization » coordinated access by more than one thread to shared state variables • Mutual Exclusion » only one thread does a particular thing at a time. One thread doing it excludes all others. • Critical Section » only one thread executes in a critical section at once 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 7

Critical Section Problem • How to share data amongst n threads in an orderly Critical Section Problem • How to share data amongst n threads in an orderly and efficient fashion • Must guarantee: » Mutual Exclusion » Progress (no deadlocks, no infinite waiting) » Bounded Waiting (a bound exists on the number of times you are skipped post request) 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 8

Locks • A lock provides mutual exclusion » Only one thread can hold the Locks • A lock provides mutual exclusion » Only one thread can hold the lock at a time » A lock is also called a mutex (for mutual exclusion) • Thread must acquire the lock before entering a critical section of code • Thread releases the lock after it leaves the critical section 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 9

Too Much Milk: A Solution YOU: YOUR ROOMMATE: Milk. Lock->Acquire(); if( milk. Amount == Too Much Milk: A Solution YOU: YOUR ROOMMATE: Milk. Lock->Acquire(); if( milk. Amount == 0 ){ // buy milk. Amount++; } } Milk. Lock->Release(); 3/19/2018 Milk. Lock->Acquire(); delay if( milk. Amount == 0 ){ // buy milk. Amount++; } } Milk. Lock->Release(); cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 10

Lock Implementation Issue • A context switch can happen at any time » very Lock Implementation Issue • A context switch can happen at any time » very simple acquire/release functions don’t work » in this case, both threads think they set lock. In. Use Lock: : Release() { lock. In. Use = false; } Lock: : Acquire() { while( lock. In. Use ) {} lock. In. Use = true; } 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington Lock: : Acquire() { while( lock. In. Use ) {} lock. In. Use = true; } 11

Disable interrupts during critical section • disable interrupts to prevent a context switch » Disable interrupts during critical section • disable interrupts to prevent a context switch » simple but imperfect solution Lock: : Acquire() { disable interrupts; } Lock: : Release() { enable interrupts; } • Kernel can’t get control when interrupts disabled • Critical sections may be long » turning off interrupts for a long time is very bad • Turning off interrupts is difficult and costly in multiprocessor systems 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 12

Disable Interrupts with flag Only disable interrupts when updating a lock flag initialize value Disable Interrupts with flag Only disable interrupts when updating a lock flag initialize value = FREE; Lock: : Acquire() { disable interrupts; while(value != FREE){ enable interrupts; disable interrupts; } value = BUSY; enable interrupts } 3/19/2018 Lock: : Release() { disable interrupts; value = FREE; enable interrupts; } cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 13

Atomic Operations • An atomic operation is an operation that cannot be interrupted • Atomic Operations • An atomic operation is an operation that cannot be interrupted • On a multiprocessor disabling interrupts doesn’t work well • Modern processors provide atomic readmodify-write instruction or equivalent • These instructions allow locks to be implemented on a multiprocessor 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 14

Examples of Atomic Instructions • Test and set (many architectures) » sets a memory Examples of Atomic Instructions • Test and set (many architectures) » sets a memory location to 1 and returns the previous value » if result is 1, lock was already taken, keep trying » if result is 0, you are the one who set it so you’ve got the lock • Exchange (x 86) » swaps value between register and memory • Compare & swap (68000) read location value if location value equals comparison value store update value, set flag true else set flag false 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 15

Busy Waiting • CPU cycles are consumed while thread is waiting for value to Busy Waiting • CPU cycles are consumed while thread is waiting for value to become 0 • This is very inefficient • Big problem if the thread that is waiting has a higher priority than the thread that holds the lock 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 16

Spinlocks are Semaphores • Used in XP, Solaris, Linux… • If( sleeping + context Spinlocks are Semaphores • Used in XP, Solaris, Linux… • If( sleeping + context switch < spinning for hundreds of cycles) then sleep(); else spin(); » In MP environment, this can be of use when the first if condition is false • If uniprocessor then sleep() else spin(); 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 17

Locks with Minimal Busy Waiting • Use a queue for threads waiting on the Locks with Minimal Busy Waiting • Use a queue for threads waiting on the lock • A guard variable provides mutual exclusion Lock: : Acquire() { while(Test. And. Set(guard)){} if( value != FREE ) { Put self on wait queue; guard = 0 and switch(); } else { value = BUSY; guard = 0; } } 3/19/2018 Lock: : Release() { while(Test. And. Set(guard){} if(anyone on wait queue){ move thread from wait queue to ready queue; } else { value = FREE; } guard = 0; } cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 18

Synchronization Summary • Threads often work independently • But sometimes threads need to access Synchronization Summary • Threads often work independently • But sometimes threads need to access shared data • Access to shared data must be mutually exclusive to ensure safety and liveness • Locks are a good way to provide mutual exclusion 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 19

Dojo-Enabled Morpheous “Is it real? ” 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 Dojo-Enabled Morpheous “Is it real? ” 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 20

Problems • Sleeping Barber • Dying Philosophers • Traveling Salesman 3/19/2018 cse 410 -23 Problems • Sleeping Barber • Dying Philosophers • Traveling Salesman 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 21

Problems • Nash Equilibrium • Talking Morpheous • Bounded-Buffer Problem 3/19/2018 cse 410 -23 Problems • Nash Equilibrium • Talking Morpheous • Bounded-Buffer Problem 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 22

Terms • • • 3/19/2018 Process Synchronization Mutual Exclusion Non-preemptive Scheduling Preemptive Scheduling Non-preemptive Terms • • • 3/19/2018 Process Synchronization Mutual Exclusion Non-preemptive Scheduling Preemptive Scheduling Non-preemptive Kernel Preemptive Kernel cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 23

Terms • Race Conditions • V. S. • Race Cars in Forza 3/19/2018 cse Terms • Race Conditions • V. S. • Race Cars in Forza 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 24

Terms • Critical Section • VS • Critical Hit 3/19/2018 cse 410 -23 -synchronization-p Terms • Critical Section • VS • Critical Hit 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 25

Terms • Process Synchronization • V. S. • Shared Memory Chaos 3/19/2018 cse 410 Terms • Process Synchronization • V. S. • Shared Memory Chaos 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 26

Terms • Non-preemptive Scheduling • V. S. • Preemptive Scheduling 3/19/2018 cse 410 -23 Terms • Non-preemptive Scheduling • V. S. • Preemptive Scheduling 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 27

Terms • Non-preemptive Kernel • V. S. • Preemptive Kernels 3/19/2018 cse 410 -23 Terms • Non-preemptive Kernel • V. S. • Preemptive Kernels 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 28

Terms • Spinlocks on uniprocessors • V. S. • Spinlocks on SMP systems 3/19/2018 Terms • Spinlocks on uniprocessors • V. S. • Spinlocks on SMP systems 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 29

Terms • Atomicity • V. S. • Mutual Exclusion 3/19/2018 cse 410 -23 -synchronization-p Terms • Atomicity • V. S. • Mutual Exclusion 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 30

Terms • Critical Section Solution Requirements • V. S. • Critically Acclaimed: Daft Punk Terms • Critical Section Solution Requirements • V. S. • Critically Acclaimed: Daft Punk 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 31

Terms • Spinlocks on single processors • V. S. • Spinlocks on multi-processors 3/19/2018 Terms • Spinlocks on single processors • V. S. • Spinlocks on multi-processors 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 32

Terms • Disabling interrupts in a uniprocessor • V. S. • Disabling interrupts in Terms • Disabling interrupts in a uniprocessor • V. S. • Disabling interrupts in a SMP system 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 33

Terms • Semaphore • V. S. • Smore 3/19/2018 cse 410 -23 -synchronization-p 1 Terms • Semaphore • V. S. • Smore 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 34

Block Locking Beats • Spinlocks • V. S. • Block[ing] locks 3/19/2018 cse 410 Block Locking Beats • Spinlocks • V. S. • Block[ing] locks 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 35

Deadlock: waiting for a call that will never come • Deadlock via wait calls Deadlock: waiting for a call that will never come • Deadlock via wait calls • V. S. • Deadlock via resource acquisition 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 36

Think and ponder… 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Think and ponder… 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 37

Terms 2 • • • 3/19/2018 Peterson’s solution Nonpreemptive uniprocessor Locks Flux capacitor Semaphores Terms 2 • • • 3/19/2018 Peterson’s solution Nonpreemptive uniprocessor Locks Flux capacitor Semaphores Mutex Mutant Ninja Turtles Dreadlocks Deadlock cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 38

Now you know… 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Now you know… 3/19/2018 cse 410 -23 -synchronization-p 1 © 2006 -07 Perkins DW Johnson and University of Washington 39