1 / 11

Thread Synchronization - Semaphores, Conditional Variable

Thread Synchronization - Semaphores, Conditional Variable. Tingxin Yan 2/23/2011. TA office hour this week. Wed 4-6 pm. Goal: Review design documents. Ways to synchronize threads. Low-level mechanisms Disable/Enable interrupts C onstraints on preemptions … High-level primitives

tybalt
Download Presentation

Thread Synchronization - Semaphores, Conditional Variable

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. Thread Synchronization- Semaphores, Conditional Variable Tingxin Yan 2/23/2011

  2. TA office hour this week • Wed 4-6 pm. • Goal: • Review design documents.

  3. Ways to synchronize threads • Low-level mechanisms • Disable/Enable interrupts • Constraints on preemptions • … • High-level primitives • Semaphores • Mutexes (Binary Semaphores) • Conditional Variables (CV)

  4. Use Semaphores and CV as examples of OS design/implementation process

  5. Semaphores in Nachos • nachos.threads.Semaphore • Suppose you are asked to implement Semaphores. • What is a semaphore? • What need to be implemented? • P() • V() • What data structures are used? • Integer value; • ThreadQueuewaitQueue;

  6. Implementation of Semaphores(1) • P() • Atomic operations if (value is zero) Put current thread to waitQueue; Current thread goes to sleep; else decrease value by 1

  7. Implementation of Semaphores(2) • V() • Atomic operations If (waitQueue is not empty) wake up the first waiting thread by adding it to the ready queue. else increase value by 1

  8. Test of Semaphores • Ping-pong test. • Create two threads; • Create two semaphores – ping and pong. • Let the status of thread alter between ping and pong. Sequence matters?

  9. Conditional Variables in Nachos • nachos.threads.Condition • nachos.threads.Condition2 (Yours!) • Suppose you are asked to implement CVs. • What is a CV? • What need to be implemented? • Sleep() • Wake()/Wakeall() • What data structures are used? • Lock (an alternative for atomic) • waitQueue of Semaphores. Each thread waiting for CV is synced by a semaphore.

  10. Implementation of CV • Left for you…

  11. Test of CV • ConditionTest.java • Producer-consumer buffer • N producers • M consumers • What CVs are needed? • Buffer empty • Buffer full • Consumer finished

More Related