260 likes | 354 Views
Synchronization Continued. Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Applied Research Laboratory Department of Computer Science and Engineering Washington University in St. Louis. Traditional UNIX kernels. Multiprogramming environment Kernel is reentrant
E N D
Synchronization Continued Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Applied Research Laboratory Department of Computer Science and Engineering Washington University in St. Louis
Traditional UNIX kernels • Multiprogramming environment • Kernel is • reentrant • nonpreemptive • Interrupt masking • interrupt priority level (ipl) • kernel can set the current ipl to block interrupts CS523 – Operating Systems
Traditional UNIX kernel • Synchronization uses two flags: locked and wanted • process wanting resource • if locked then sleep, otherwise set locked flag • process holding resource • when done, • if wanted flag, then call wakeup on wait channel • How is access to these two flags synchronized? CS523 – Operating Systems
Wait Channel • sleep (wait_channel, priority) • special wait channels • lbolt - awakened by scheduler once per second • wait syscall - parent waits on its proc structure • sigpause syscall - wait on user structure (u.area) • wakeup (wait_channel) - all processes sleeping on a wait channel are made ready - Why? CS523 – Operating Systems
Resource X locked: 1 wanted: 1 P7 Wait Channels and Sleep Qs Owns resource 0xf1023450 Sleep Queues Hash function Index P4 P5 P6 X X X Queue of processes “waiting” for some resources which hashes to this index value. CS523 – Operating Systems
Wait Channel Limitations • Multiple events map to the same wait channel • Multiple resources map to the same wait channel • Does not support more complex protocols such as readers-writers CS523 – Operating Systems
Resource X locked: 1 wanted: 1 Wait Channels - Limitations Sleep Queues Hash function Resource Y P4 P5 P6 locked: 1 Y X X wanted: 1 CS523 – Operating Systems
Multiprocessor Support • The traditional approaches do not work. • Lost wakeup problem • thundering heard problem • Hardware support for synchronization • Atomic test-and-set: test bit, set it to 1, return old value • load-linked and store-conditional (read-modify-write): CS523 – Operating Systems
MP Synchronization • Initial Implementations • Semaphores • Current Trends • Spin Locks • Condition Variables • Read-Write Locks • Reference Locks CS523 – Operating Systems
Semaphores • Integer variable supporting two operations : • wait(S): • if S 1 then S := S - 1 and return, • else block thread on the semaphore queue • signal(S): • if blocked threads, wake them up • else S := S + 1 • Kernel guarantees operations are atomic • Can define a non-blocking version of try(CP). • Threads are woken up in FIFO order - • Semaphore convoys - results in unnecessary context switches. • How would you use semaphores to implement mutual exclusion or event waiting? • Mutual Exclusion: binary semaphore initialized to 1 • Event Waiting: binary semaphore initialized to 0 • How would you implement a resource counting semaphore? • Initialized to number of available resources CS523 – Operating Systems
Semaphore Implementations void wait (sem *s) { *s -= 1; while (*s < 0) sleep; } void signal (sem *s) { *s += 1; if (*s <= 0) wakeup one blocked thread; } void initsem (sem *s, int val) { *s = val; } boolean_t trylock (sem *s) { if (*s > 0) { *s -= 1; return TRUE; } else return FALSE; } CS523 – Operating Systems
Semaphores - Drawbacks • High level abstraction built using lower-level primitives • Requires blocking/unblocking and sleep queue management - expensive • Hides whether thread has blocked CS523 – Operating Systems
Other Synchronization Mechanisms • Mutex - Mutual Exclusion Lock, applies to any primitive that enforces mutual exclusion semantics • Condition variables • Read/Write Locks • Reference Counting CS523 – Operating Systems
Spin Locks (MP Support) • The idea is to provide a basic, HW supported primitive with lowoverhead. • Lock held for short periods of time • If locked, then busy-wait on the resource • Must not give up processor! In other words, can not block. void spin_lock (spinlock_t *s) { while (test_and_set (s) != 0) while (*s != 0) ; } void spin_unlock (spinlock_t *s) { s = 0; } CS523 – Operating Systems
Blocking Locks/Mutex • Allows threads to block • Interface: lock(), unlock () and trylock () • Consider traditional kernel locked flag • Mutex allows for exclusive access to flag, solving the race condition • flag can be protected by a spin lock. CS523 – Operating Systems
predicate Condition variable mutex List of blocked threads mutex (for list) kthread_3 kthread_2 kthread_1 Condition Variables update predicate wake up one thread Thread sets event CS523 – Operating Systems
Condition Variables • Associated with a predicate which is protected by a mutex (usually a spin lock). • Useful for event notification • Can wakeup one or all sleeping threads! • Up to 3 or more mutex are typically required: • one for the predicate • one for the sleep queue (or CV list) • one or more for the scheduler queue (swtch ()) • deadlock avoided by requiring a strict order CS523 – Operating Systems
CV Implementation void signal (cv *c) { lock (&cv->listlock); remove a thread from list unlock (&cv->listlock); if thread, make runnable; return; } void broadcast (cv *c) { lock (&cv->listlock); while (list is nonempty) { remove a thread make it runnable} unlock (&cv->listlock); return; } void wait (cv *c, mutex_t *s) { lock (&cv->listlock); add thread to queue unlock (&cv->listlock); unlock (s); swtch (); /* return => after wakup */ lock (s); return; } CS523 – Operating Systems
Reader/Writer Locks • Many readers, one writer • Writer can only acquire lock if no readers are active - lockexclusive (rwlock_t *r) • Any reader can acquire lock if no writers are active - lockshared (rwlock_t *r) • Writer release lock, do we wake up all readers or a writer? CS523 – Operating Systems
RW Locks • Preferred solution, • writer wakes up all sleeping readers, • readers do nothing if there are still active readers. • But, this could lead to writer starvation, avoided if lockshared fails when there is a waiting writer. • What about when a reader wants to upgrade to an exclusive lock? • must give preference to reader's upgrade • multiple upgrade requests are problematic, could return fail and release lock. CS523 – Operating Systems
Reference Counts • Protects object when outstanding reference • Consider the case where process/kernel as reference to data object which has been reallocated for another purpose. • fix: kernel reference counts objects. • When kernel provides reference to object it increments the reference count • when process releases the object then reference count is decremented. • when reference count goes to zero, there are guaranteed to be no outstanding references to the object. CS523 – Operating Systems
Considerations • Deadlock avoidance: • hierarchical - impose order on locks • stochastic locking - when violating hierarchy, use trylock. • Recursive Locks - attempting to acquire a lock already owned by a thread succeeds. • Adaptive Locks (Mutex) • depending on circumstances and performance considerations, either a spin or blocking lock is used. • Protecting invariants, predicates, data, operations (monitors). • Granularity and Duration. CS523 – Operating Systems
SVR4.2/MP • Basic Locks: nonrecursive mutex, short-term, thread may not block • Read-Write Locks: nonrecursive; short-term; thread may not block; single-writer, multiple reader semantics. • Sleep Locks: nonrecursive locks, long-term, thread may block. • Synchronization variable (condition variable). CS523 – Operating Systems
Solaris • Types: • Mutexes - spin, adaptive. • Condition Variables • Counting Semaphores • Multiple-Reader, Single-Writer • Queuing mechanism: • turnstiles - used for mutexes, reader/writer and semaphores • sleep queues - condition variables. CS523 – Operating Systems
Digital UNIX • Simple Lock - spin lock • Complex Lock - complex, high-level abstraction. CS523 – Operating Systems
NT • Spin lock, kernel & executive only. • Executive resources. Provide exclusive and shared read. Kernel mode only. • Dispatcher object (kernel supplies to executive). Exposed to win32 apps. Essentially an event queue. Only two states: Signaled or NonSignaled. CS523 – Operating Systems