Synchronization
Synchronization
 Past:
                                                               What an OS is, why we have them, what they do.
      Thread Synchronization                                
                                                            
                                                                Base hardware and support for operating systems
                                                                Process Management
                                                               Process Scheduling
                                                               Multi-Threading
                 Dickinson College                        Present:
               Computer Science 354                          Thread Synchronization
                                                          Future:
                    Spring 2006                              Memory management
                                                             Storage management
                                                             Protection and Security
            slides courtesy of Professor Grant Braught
                                                                                                                     1
          Random OS Humor                                                Terminology
Windows Haiku Error Messages                         Thread synchronization
                                                        Mutual Exclusion
  Three things are certain:                               Critical sections
   Death, taxes and lost data.                          Serialization
   Guess which has occurred.                            Race condition
                                                        Non-determinism
  Having been crashed
   The document you're seeking
   Must now be retyped.
                                                                                                                          2
              Serialization                              Serialization Example
A serialization constraint requires that two    Consider two threads, threadA that generates a
                                                  value of X and threadB that uses the value of X
 or more events be forced to execute in a         to calculate the value of Y.
 specific order.                                    Assume: X=1, Y=0 are stored in the address space
                                                     shared by the threads.
                                                     threadA:                        threadB:
                                                        a1: X = 5                         b1: Y = X + 3
                                                        a2: Print X                       b2: Print Y
        Random OS Humor
Alternative acronyms:
  PCMCIA: People Can't Memorize Computer
   Industry Acronyms
                                                      Thread Synchronization
  SCSI: System Can't See It                        from the User's Perspective
  DOS: Defective Operating System
  BASIC: Bill's Attempt to Seize Industry
   Control
                                                                                                                    3
                    Outline                                       Semaphores
 Synchronization mechanisms
   Semaphore                                 A semaphore is an integer with three
   Others                                     differences:
 Synchronization patterns                       When created, its value must be initialized,
   Signaling                                     thereafter the only operations allowed are
   Rendezvous                                    increment and decrement.
   Mutex                                        When a thread decrements a semaphore, if
   Multiplex                                     the result is negative the thread is blocked on
   Barrier                                       the semaphore.
 Synchronization problems                       When a thread increments a semaphore, if its
   Producer-Consumer                             value was negative then one of the threads
   Readers-Writers                               blocked on the semaphore is woken up.
   Unisex Bathroom
Decrementing a semaphore:
                                               Increment:
     mySem.wait();
                                                mySem.increment();
                                                mySem.V();
Incrementing a semaphore:
                                                mySem.incrementAndWakeAWaitingThreadIfAny();
     mySem.signal();
      Alternative Mechanisms
In addition to semaphores there are a
 variety of other mechanisms that are also
 used for thread synchronization:
                                                               Synchronization
  Locks                                                          Patterns
  Monitors
     Condition Variables
                                                                                                    4
            Signaling Pattern                                        Rendezvous Pattern
                                                            Rendezvous generalizes signaling to work both
The signaling pattern can be used to                        ways.
                                                               E.g. Thread A must complete section a1 before
 enforce a serialization constraint on two                      thread B executes section b2 and thread B must
 threads.                                                       complete section b1 before thread A completes
  E.g. Thread A must complete section a1                       section a2.
   before thread B executes section b2.                        How can Rendezvous be implemented using
   Shared Data                                                  semaphores?
   1. mySem = new Semaphore(0);
                                                              Thread A                        Thread B
   Thread A                   Thread B                        1. section a1                   1. section b1
   1. section a1              1. section b1                   2. aReady.signal();             2. bReady.signal();
   2. mySem.signal();         2. mySem.wait();                3. bReady.wait();               3. aReady.wait();
   3. section a2              3. section b2                   4. section a2                   4. section b2
                                                                                                                          5
        Barrier Non-Solution #1                                                 Barrier Solution
What is wrong with the following proposed
 solution for the barrier problem?                          The following code solves the Barrier
                                                             problem.
Shared Data                      Thread A
                                                            Shared Data                               Thread A
1. int N;                        1. section a1
                                                            1. int N;                                 1. section a1
2. int count = 0;                2. mutex.wait();
3. mutex = new Semaphore(1);                                2. int count = 0;                         2. mutex.wait();
                                 3.  count++;               3. mutex = new Semaphore(1);
4. barrier = new Semaphore(0);                                                                        3.  count++;
                                 4. mutex.signal();         4. barrier = new Semaphore(0);            4. mutex.signal();
                                 5. if (count == N)
                                                                                                      5. if (count == N)
                                 6.   barrier.signal();
                                                                                                      6.   barrier.signal();
                                 7. barrier.wait();
                                                                                                      7. barrier.wait();
                                 8. section a2                                                        8. barrier.signal();
                                                                                     Turnstile        9. section a2
                                                                 Producer-Consumer Problem
                                                             Producer Threads: produce items and adds
                                                              them to a shared data structure.
                Synchronization                              Consumer Threads: remove items from a shared
                                                              data structure and processes them.
                   Problems
                                                            Producer                             Consumer
                                                            1.   while (true)                    1.   while (true)
                                                            2.    section p1                     2.    section c1
                                                            3.    pItem = generateItem();        3.    cItem = buffer.get();
                                                            4.    buffer.add(pItem);             4.    processItem(cItem);
                                                            5.    section p2                     5.    section c2
                                                                                                                               6
                                                               Producer-Consumer Non-
  Producer-Consumer Solution
  Solution:
                                                                       Solution
  mutex = new Semaphore(1);                              What’s wrong with the following code for
  items = new Semaphore(0);
                                                          the Consumer threads?
Producer:                     Consumer:
                                                               Consumer
                                                               1. while (true)
while (true)                  while (true)                     2.  section c1
   Section p1                    Section c1                    3.  mutex.wait();
                                                               4.  items.wait();
   pItem = generateItem();       items.wait();                 5.  cItem = buffer.get();
   mutex.wait();                 mutex.wait();                 6.  mutex.signal();
   buffer.add(pItem);            cItem = buffer.get();         7.  process(cItem);
                                                               8.  section c2
   items.signal();               mutex.signal();
   mutex.signal();               processItem(cItem);
   Section p2                    Section c2
                                                                Readers-Writers Solution
      Readers-Writers Problem                            Shared Data
                                                         1. readers = 0;
 Readers Threads: read information from a               2. mutex = new semaphore(1);
                                                         3. roomEmpty = new semaphore(1);
  shared data structure (database / variable / file
  etc…)                                                   Writers:                         Readers:
 Writers Threads: write information to a shared          1. section w1                    1. section r1
                                                          2. roomEmpty.wait();             2. mutex.wait();
  data structure.                                         3.   write data                  3.    readers++;
        Writer:              Reader:                      4. roomEmpty.signal();           4.    if readers == 1
        1.   section w1      1.   section r1              5. section w2                    5.      roomEmpty.wait();
        2.   write data      2.   read data                                                6. mutex.signal();
        3.   section w2      3.   section r2
                                                                                           7. read data
                                                                                           8. mutex.wait();
                                                                                           9. readers--;
                                                                                           10. if readers == 0
    What are the synchronization constraints in this                                      11.      roomEmpty.signal();
     problem?                                                                              12. mutex.signal();
                                                                                                                          7
               Lightswitch Pattern                                             Unisex Bathroom Problem
 Shared Data
 1.   inRoom = 0;
 2.   mutex = new semaphore(1);                                            A high-tech startup company can only afford space with
 3.   light = new semaphore(1);                                             a single bathroom with 3 stalls. A CS major working for
                                                                            the company proposes to solve the problem of allowing
 Thread A:                                                                  both men and women to use the bathroom using
 1.   section a1
                                                                            semaphores.
 2.   mutex.wait();
 3.    inRoom++;                                                              Synchronization constraints:
 4.    if inRoom == 1 // first in…                                                  Men and women cannot be in the bathroom at the same time.
 5.      light.wait(); // turn on light if off (or block)
 6.   mutex.signal();                                                               There should never be more than 3 people in the bathroom at once.
 7.   Critical Section
                                                                              Man Thread:                        Woman Thread:
                                                                              1.    while (true)                 1.   while (true)
 8. mutex.wait();                                                             2.     doWork();                   2.    doWork();
 9.   inRoom--;                                                               3.     goToBathroom();             3.    goToBathroom();
 10. if inRoom == 0            // last out…
 11.    light.signal();        // turn off light
 12. mutex.signal();
 13. section a2
                                                                                                        Outline
                                                                          How do semaphores do what they do?
                                                                             Basic semaphore structure
            Synchronization                                                        A new critical section!
        from the OS Perspective                                              Semaphore implementations
                                                                          Other synchronization mechanisms
                                                                                                                                                         8
   Semaphores in Kernel Mode                              Hardware and Mutual Exclusion
Semaphores can be implemented in
                                                          Additional instructions can be provided by
 kernel mode by disabling and enabling
                                                           the hardware to enable mutual exclusion
 interrupts.
                                                           without disabling interrupts.
semWait() {                      semSignal() {
  disable interrupts               disable interrupts
                                                             One such instruction is TSL (test and set
  value = value - 1;               value = value + 1;         lock):
  if (value < 0) {                 if (value <= 0) {                                   reg = mm[addr]
    enable interrupts                wakeup a thread                TSL addr           mm[addr] = true
    block calling thread           }
  }
                                                                                       return reg
                                   enable interrupts
  enable interrupts              }
}
                                                                 TSL like all machine language instructions is
   Semaphore creation as well as semWait and                     executed atomically.
    semSignal must be system calls.
                                                                                                                        9
                                                                                       Other Synchronization
              Random OS Humor
                                                                                           Mechanisms
"DOS computers manufactured by                                            Semaphores are only one mechanisms for
 companies such as IBM, Compaq, Tandy,                                      enforcing synchronization constraints.
 and millions of others are by far the most                                 There are several others:
 popular, with about 70 million machines in
                                                                             Locks
 use worldwide. Macintosh fans, on the
 other hand, may note that cockroaches                                       Monitors
 are far more numerous than humans, and                                            Synchronization in Java
 that numbers alone do not denote a higher
 life form."
                             Locks                                                                           Monitors
A lock is a mechanism for enforcing
 mutual exclusion.                                                         Code within a monitor may only be
                                                                            executed by a single thread at a time.
 Shared:
 Lock mutex = new Lock();                                                    monitor <name> {
 int x = 0;
                                                                                 <shared variables>
 Thread A:                             Thread B:                                 initialization(<params>) {
 mutex.lock();                         mutex.lock();                               // initialization code
 /* Critical section */                /* Critical section */                    }
 x = x + 1;                            x = x - 1;                                procedure P1(<params>) {
 mutex.unlock();                       mutex.unlock();                             // code in P1
                                                                                 }
                                                                                 procedure P2(<params>) {
   Locks can be implemented using:                                                // code in P2
        Enable/disable interrupts and blocking                                  }
                                                                                 …
        Busy-waiting with TSL                                               }
                                                                                                                               10
                                                             Java Semaphore
       Java Synchronization
                                                              Implementation
                                                        public class Semaphore {
Synchronization in Java is accomplished                    private int value;
11