Upload
roland-montgomery
View
212
Download
0
Embed Size (px)
Citation preview
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 1
Synchronization Part 1
CSE 410, Spring 2008
Computer Systems
http://www.cs.washington.edu/410
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 2
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
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 3
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…
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 4
Too Much Milk
Arrive home; put milk away
Oh no, too much milk!
3:30
Buy milk3:25
Arrive at storeArrive home; put milk away3:20
Leave for storeBuy milk3:15
Look in fridge; no milkArrive at store3:10
Leave for store3:05
Look in fridge; no milk3:00
Your RoommateYou
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 5
Modeling the Problem
• Model you and your roommate as threads
• “Looking in the fridge” and “putting away milk” are reading/writing a variable
YOU:
// look in fridgeif( milkAmount == 0 ) { // buy milk milkAmount++; }
YOUR ROOMMATE:
// look in fridgeif( milkAmount == 0 ) { // buy milk milkAmount++; }
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 6
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!)
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 7
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
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 8
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)
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 9
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
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 10
Too Much Milk: A Solution
YOU:
MilkLock->Acquire();if( milkAmount == 0 ){ // buy milk milkAmount++; }}MilkLock->Release();
YOUR ROOMMATE:
MilkLock->Acquire();
delay
if( milkAmount == 0 ){ // buy milk milkAmount++; }}MilkLock->Release();
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 11
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 lockInUse
Lock::Acquire() { while( lockInUse ) {} lockInUse = true;}
Lock::Release() { lockInUse = false;}
Lock::Acquire() { while( lockInUse ) {} lockInUse = true;}
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 12
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
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 13
Disable Interrupts with flag
Only disable interrupts when updating a lock flag
Lock::Release() { disable interrupts; value = FREE; enable interrupts;}
initialize value = FREE;
Lock::Acquire() { disable interrupts; while(value != FREE){ enable interrupts; disable interrupts; } value = BUSY; enable interrupts}
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 14
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 read-modify-write instruction or equivalent
• These instructions allow locks to be implemented on a multiprocessor
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 15
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 (x86)» swaps value between register and memory
• Compare & swap (68000)read location valueif location value equals comparison value
store update value, set flag trueelse
set flag false
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 16
Busy Waiting
• CPU cycles are consumed while the 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
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 17
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();
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 18
Locks with Minimal Busy Waiting
• Use a queue for threads waiting on the lock• A guard variable provides mutual exclusion
Lock::Release() { while(TestAndSet(guard){} if(anyone on wait queue){ move thread from wait queue to ready queue; } else { value = FREE; } guard = 0;}
Lock::Acquire() { while(TestAndSet(guard)){} if( value != FREE ) { Put self on wait queue; guard = 0 and switch(); } else { value = BUSY; guard = 0; } }
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 19
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
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 20
Dojo-Enabled Morpheous
“Is it real?”
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 21
Problems
• Sleeping Barber
• Dying Philosophers
• Traveling Salesman
Problems
• Nash Equilibrium
• Talking Morpheous
• Bounded-Buffer Problem
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 22
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 23
Terms
• Process Synchronization• Mutual Exclusion• Non-preemptive Scheduling• Preemptive Scheduling• Non-preemptive Kernel • Preemptive Kernel
Terms
• Race Conditions
• V.S.
• Race Cars in Forza
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 24
Terms
• Critical Section
• VS
• Critical Hit
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 25
Terms
• Process Synchronization
• V.S.
• Shared Memory Chaos
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 26
Terms
• Non-preemptive Scheduling
• V.S.
• Preemptive Scheduling
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 27
Terms
• Non-preemptive Kernel
• V.S.
• Preemptive Kernels
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 28
Terms
• Spinlocks on uniprocessors
• V.S.
• Spinlocks on SMP systems
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 29
Terms
• Atomicity
• V.S.
• Mutual Exclusion
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 30
Terms
• Critical Section Solution Requirements
• V.S.
• Critically Acclaimed: Daft Punk
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 31
Terms
• Spinlocks on single processors
• V.S.
• Spinlocks on multi-processors
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 32
Terms
• Disabling interrupts in a uniprocessor
• V.S.
• Disabling interrupts in a SMP system
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 33
Terms
• Semaphore
• V.S.
• Smore
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 34
Block Locking Beats
• Spinlocks
• V.S.
• Block[ing] locks
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 35
Deadlock: waiting for a call that will never come
• Deadlock via wait calls
• V.S.
• Deadlock via resource acquisition
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 36
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 37
Think and ponder…
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 38
Terms2
• Peterson’s solution• Nonpreemptive uniprocessor• Locks• Flux capacitor• Semaphores• Mutex• Mutant Ninja Turtles• Dreadlocks• Deadlock
04/19/23 cse410-23-synchronization-p1 © 2006-07 Perkins DW Johnson and University of Washington 39
Now you know…