1 / 30

Synchronization and IPC Mechanisms

Synchronization and IPC Mechanisms. Fred Kuhns. Traditional UNIX kernels. Multiprogramming environment Kernel is reentrant nonpreemptive Interrupt masking interrupt priority level ( ipl ) kernel can set the current ipl to block interrupts. Traditional UNIX kernel.

kim
Download Presentation

Synchronization and IPC Mechanisms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Synchronization and IPC Mechanisms Fred Kuhns CS523S: Operating Systems

  2. Traditional UNIX kernels • Multiprogramming environment • Kernel is • reentrant • nonpreemptive • Interrupt masking • interrupt priority level (ipl) • kernel can set the current ipl to block interrupts CS523S: Operating Systems

  3. 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? CS523S: Operating Systems

  4. 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 awoken - Why? CS523S: Operating Systems

  5. Resource X locked: 1 wanted: 1 P7 Wait Channels and Sleep Qs Owns resource 0xffffffff Sleep Queues Hash function P4 P5 P6 X X X CS523S: Operating Systems

  6. 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 CS523S: Operating Systems

  7. 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 CS523S: Operating Systems

  8. Solaris: Condition Variables • Solaris implements sleep/wakeup semantics using condition variables • cv_wait(), cv_wait_sig (), cv_timedwait() • Sleep queue: priority order • wakeup one or all threads sleeping on an object. CS523S: Operating Systems

  9. 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): CS523S: Operating Systems

  10. MP Synchronization • Initial Implementations • Semaphores • Current Trends • Spin Locks • Condition Variables • Read-Write Locks • Reference Locks CS523S: Operating Systems

  11. 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 • Each Semaphore has an associated queue. • Kernel guarantees operations are atomic • Can define a non-blocking version of try(CP). CS523S: Operating Systems

  12. More on Semaphores • 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 CS523S: Operating Systems

  13. 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; } CS523S: Operating Systems

  14. Semaphores - Drawbacks • High level abstraction built using lower-level primitives • Requires blocking/unblocking and sleep queue management - expensive • Hides whether thread has blocked CS523S: Operating Systems

  15. Other Synchronization Mechanisms • Mutex - Mutual Exclusion Lock, applies to any primitive that enforces mutual exclusion semantics • Condition variables • Read/Write Locks • Reference Counting CS523S: Operating Systems

  16. Spin Locks or Simple Mutexes • The idea is to provide a basic, HW supported primitive with low overhead. • 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. CS523S: Operating Systems

  17. Spin-Lock implementation void spin_lock (spinlock_t *s) { while (test_and_set (s) != 0) while (*s != 0) ; } void spin_unlock (spinlock_t *s) { s = 0; } CS523S: Operating Systems

  18. 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. CS523S: Operating Systems

  19. 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 CS523S: Operating Systems

  20. 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; } CS523S: Operating Systems

  21. 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 CS523S: Operating Systems

  22. 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? CS523S: Operating Systems

  23. 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. CS523S: Operating Systems

  24. RW Locks struct rwlock { int nActive; int nPendingReads; int nPendingWrites; spinlock_t sl; condition canRead; condition canWrite; } CS523S: Operating Systems

  25. 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. CS523S: Operating Systems

  26. 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. CS523S: Operating Systems

  27. 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). CS523S: Operating Systems

  28. 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. CS523S: Operating Systems

  29. Digital UNIX • Simple Lock - spin lock • Complex Lock - complex, high-level abstraction. CS523S: Operating Systems

  30. 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. CS523S: Operating Systems

More Related