Semaphores in Operating System


Semaphores is an important topic of Operating System and many students even teachers have confusions about this topic. I will teach you everything about Semaphores with a very simple and easy explanation but the condition is that you will have to read it till the end. So,let's start, shall we?

The most simple definition of Semaphore is that it is just an integer variable which is used to solve critical section problem and to achieve process synchronization.

Semaphores has 2 functions namely wait() and signal() to perform process synchronization. Let's discuss about them one by one:

1. Wait() : Wait() simply decrements the value of semaphore variable S if it is greater than 0. Otherwise, if value of S becomes less than or equal to 0 then that variable S is trapped in a loop. Now, the usage of this we'll see later on.
code:
        wait(S)
        {
                while(S<=0);
                S--;
        }

2. Signal() : Signal() simply increments the value of semaphore variable S.
code:
         signal(S)
         {
                 S++;
         }

Solving Critical Section Problem with Semaphores

Semaphores in Operating System

This was a basic diagram of a various sections of a code. Now, I will modify it a little bit to show you how semaphores can be implemented into it.

Semaphores in Operating System

In the above picture we have used wait() and signal(). Now, let's see if semaphore solves problem of critical section or not by checking if they follow 3 criterias of critical section problem.
1. Let say initially value of S is 1 and we have 3 process P1, P2 and P3.
2. P1 comes and initially value of S is 1. P1 will call wait(). Now,  wait() will execute while loop for S<=0 which is not true so value of S is decremented to 0 and from the above picture P1 will enter critical section.
3. Next when P2 comes the value of S is 0. P2 will call wait(). Now, wait() will execute while loop for S<=0 which is true this time so P2 will be trapped in while loop until S becomes greater than 0. This shows that at a time only one process can enter critical section hence Mutual Exclusion property is fulfilled. 
4. When P1 will come out of critical section then it will call signal(). Now, signal will increment the value of S by 1 so now value of S becomes 1. Since, value of S is now 1 so P2 will come out of the while loop as well and now it will enter critical section.
5. Now, let's discuss about Progress. Actually, progress is ensured automatically because the process which wants to enter critical section will enter while loop until the process inside critical section don't come out. So, the process which wants to enter critical section will turn value of S from 1 to 0 .
6. Now, let's check about Bounded Waiting . When a process comes in critical section then it turns 1 to 0 and when it leaves critical section then it makes it 1. So, is it possible that a process which enters critical section once will enter into it again? The answer is YES. The new process which will enter critical section is purely random in semaphores. Hence there is a chance that a process which has executed in critical section can enter into it again. So, bounded waiting is not ensured in semaphores.

Types of Semaphores


  1. Counting Semaphores : In these semaphores a count is maintained which denotes number of available resources. If a resource is added then count is incremented and if resource is removed then count is decremented.
  2. Binary Semaphores : Binary semaphore can have values 0 or 1. If wait operation works when value of semaphore is 1 and signal operation works when semaphore is 0.

2 Comments

Post a Comment
Previous Post Next Post