CS 551: Distributed Operating Systems
Example of Semaphore Use in DS


Example of Semaphore Use in DS

Now consider a distributed system
    with three running processes
Each of the processes has access
    to a shared semaphore, S
Each process has a critical section
    controlled by S
Assume the semaphore/lock is controlled
    by a centralized lock manager

Initially S = 1
    with no processes are in their critical section

Steps:
  1. Process 3 requests its critical section
        Since S = 1, S !<= 0
            So S is decremented by 1
        making S = 0
    and
  2. Process 3 is granted access to its critical section

  3. Process 1 requests its critical section
        But S <= 0, as S = 0
    so
  4. So Process 1 is is placed on a queue
        until S > 0

  5. Process 2 requests its critical section
        But S <= 0, as S = 0
    so
  6. Process 2 is queued
        until S > 0

  7. Process 3 finishes its critical section
        So S is incremented by 1
            making S = 1
        releasing access to the critical section

  8. The central manager checks the semaphore value S
        Since S = 1, S !<= 0
            So S is decremented by 1
        making S = 0
    and Process 1 is granted access to its critical section
        by the central manager

  9. Process 1 finishes its critical section
        So S is incremented by 1
            making S = 1
        releasing access to the critical section

  10. The central manager checks the semaphore value S
        Since S = 1, S !<= 0
            So S is decremented by 1
        making S = 0
    and Process 2 is granted access to its critical section
        by the central manager

  11. Process 2 finishes its critical section
        So S is incremented by 1
            making S = 1
        releasing access to the critical section



Comments:
Copyright © 2001-2003: Colorado State University for CS 551. All rights reserved.