SEMAPHORES

SEMAPHORE:
  • A semaphore is hardware or a software tag variable whose value indicates the status of a common resource.
  • Its purpose is to lock the resource being used.
  • A process which needs the resource will check the semaphore for determining the status of the resource followed by the decision for proceeding.
  • In multitasking operating systems, the activities are synchronized by using the semaphore techniques.
  • So it is basically a synchronizing tool and is accessed only through two low standard atomic operations, wait and signal designated by P() and V() respectively.

Semaphores support two operations:

wait(semaphore): decrement, block until semaphore is open
signal(semaphore): increment, allow another thread to enter

The definition of wait() is as follows:

Wait(S){
While S<=0
    ;// no operation
S--;
}

The definition of Signal() is as follows:

Signal (S){
S++;
}

PROPERTIES OF SEMAPHORES:
1. Simple
2. Works with many processes
3. Can have many different critical sections with different semaphores
4. Each critical section has unique access semaphores
5. Can permit multiple processes into the critical section at once, if desirable

TYPES OF SEMAPHORES:
Semaphores are mainly of two types:
            Binary semaphores
            Counting semaphores

Binary Semaphore:

  • It is a special form of semaphore used for implementing mutual exclusion, hence it is often called Mutex.
  • A binary semaphore is initialized to 1 and only takes the value 0 and 1 during execution of a program.
  • Binary semaphores have 2 methods associated with it. (up, down / lock, unlock)
  • They are used to acquire locks. When a resource is available, the process in charge set the semaphore to 1 else 0.

Counting Semaphores:
  • These are used to implement bounded concurrency.
  • Counting Semaphore may have value to be greater than one, typically used to allocate resources from a pool of identical resources.

   Drawbacks:

  • They are essentially shared global variables
  • Can potentially be accessed anywhere in program
  • No connection between the semaphore and the data being controlled by the semaphore
  • Used both for critical sections (mutual exclusion) and coordination (scheduling)
  • No control or guarantee of proper usage
  • Sometimes hard to use and prone to bugs
  • With improper use, a process may block indefinitely. Such a situation is called Deadlock. 



No comments:

Post a Comment