Semaphores in Operating System

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

Integer variables that help solve the critical section problem are known as semaphores. Wait and signal are the two atomic operations that help achieve process synchronization in a multiprocessing environment..

If an argument, let’s say argument A is positive, then the wait (sleep or down) operation decrements its value. And if argument A is negative or zero, then no operation occurs. Whereas, the signal (wake-up or up) operation increments the value of argument A.

P(A) is wait and V(A) is signal.

P(A): if A >= 1 then A := A - 1
      else <block and enqueue the process>;

V(A): if <some process is blocked on the queue>
        then <unblock a process>
      else A := A + 1;

Properties of Semaphores

Following are the properties of semaphore:

  • Simple and has a positive integer value.
  • Can work with multiple processes.
  • Different critical sections for different semaphores.
  • An unique access semaphore for each critical section.
  • Allows multiple processes into the critical section at a time, if required.

Types of Semaphore

There are two types of semaphores:

1. Counting Semaphore

Counting semaphores are integer value semaphores. They have an unrestricted value domain and are used to coordinate resource access. During this coordination, the count of semaphores is equal to the number of available resources and if a resource is added or removed, then the count of semaphores changes accordingly.

2. Binary Semaphore

Binary semaphores are similar to the above, just that their value is restricted to 0 and 1. The wait and signal operation only works when the semaphore is 1 and 0 respectively. Binary semaphores are easier to implement.

Counting Semaphore vs Binary Semaphore

COUNTING SEMAPHOREBINARY SEMAPHORE
No mutual exclusionMutual exclusion is present
Any integer value possibleTwo integer values, 0 and 1
More than one slotsOnly one slot
Offers a set of processesContains mutual exclusion mechanism

Semaphore vs Mutex

PARAMETERSEMAPHOREMUTEX
Data TypeInteger variableObject
MechanismSignaling mechanismType of locking mechanism
TypesBinary and Counting No Type
OperationWait and signal modifies the semaphore’s value Locked and Unlocked operation
ThreadMultiple program threadsMultiple program threads that aren’t simultaneous

Advantages of OS Semaphores

Following are the advantages of semaphore:

  • They allow one process into the critical section at a time and follow the principle of mutual exclusion. They are far more efficient than other methods of synchronization.
  • Zero wastage of resources due to busy waiting in semaphore.
  • The OS implements a semaphore in the machine-independent code of the microkernel. This makes them machine-independent.

Disadvantages of Semaphore in Operating System

Following are the disadvantages of semaphores:

  • It is necessary to implement the wait and signal operations in the correct order in order to prevent deadlocks.
  • They are impractical for last-scale use. This is because it leads to loss of modularity as wait and signal operations don’t allow the creation of a structured layout for the system.
  • Semaphores can cause a priority inversion where low priority processes may get the chance to access the critical section before the high priority processes.

Summary

A semaphore is a variable that helps with critical section problems and process synchronisation. Wait and signal are the two operations that help modify the value of a semaphore. There are two types of semaphores, namely, counting and binary.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *