What is semaphore and what are its types?

In a system, we have a limited amount of resources that are being shared between various processes. One resource should be used by only one process at a time. This is called process synchronization. So, in an Operating System, we must have synchronization between various processes. This synchronization between processes can be achieved with the help of semaphore. So, in this blog, we will learn about semaphore and we will also look at the types of a semaphore.

Before starting this blog, you should know the concept of Process Synchronization(read the process synchronization blog from here ).

Semaphore

A semaphore is a variable that indicates the number of resources that are available in a system at a particular time and this semaphore variable is generally used to achieve the process synchronization. It is generally denoted by " S ". You can use any other variable name of your choice.

A semaphore uses two functions i.e. wait() and signal() . Both these functions are used to change the value of the semaphore but the value can be changed by only one process at a particular time and no other process should change the value simultaneously.

The wait() function is used to decrement the value of the semaphore variable " S " by one if the value of the semaphore variable is positive. If the value of the semaphore variable is 0, then no operation will be performed.

wait(S) {
    while (S == 0); //there is ";" sign here
    S--;
}

The signal() function is used to increment the value of the semaphore variable by one.

signal(S) {
    S++;
}

Types of Semaphore

There are two types of semaphores:

  • Binary Semaphores: In Binary semaphores, the value of the semaphore variable will be 0 or 1. Initially, the value of semaphore variable is set to 1 and if some process wants to use some resource then the wait() function is called and the value of the semaphore is changed to 0 from 1. The process then uses the resource and when it releases the resource then the signal() function is called and the value of the semaphore variable is increased to 1. If at a particular instant of time, the value of the semaphore variable is 0 and some other process wants to use the same resource then it has to wait for the release of the resource by the previous process. In this way, process synchronization can be achieved.
  • Counting Semaphores: In Counting semaphores, firstly, the semaphore variable is initialized with the number of resources available. After that, whenever a process needs some resource, then the wait() function is called and the value of the semaphore variable is decreased by one. The process then uses the resource and after using the resource, the signal() function is called and the value of the semaphore variable is increased by one. So, when the value of the semaphore variable goes to 0 i.e all the resources are taken by the process and there is no resource left to be used, then if some other process wants to use resources then that process has to wait for its turn. In this way, we achieve the process synchronization.

Advantages of semaphore

  • The mutual exclusion principle is followed when you use semaphores because semaphores allow only one process to enter into the critical section.
  • Here, you need not verify that a process should be allowed to enter into the critical section or not. So, processor time is not wasted here.

Disadvantages of semaphore

  • While using semaphore, if a low priority process is in the critical section, then no other higher priority process can get into the critical section. So, the higher priority process has to wait for the complete execution of the lower priority process.
  • The wait() and signal() functions need to be implemented in the correct order. So, the implementation of a semaphore is quite difficult.

This is all about semaphores. Hope you learned something new today.

Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here .

Keep Learning :)

Team AfterAcademy!