Semaphore in Operating System (OS) (What is)

Semaphore in Operating System

Semaphore is also an entity devised by Edsger W. Dijkstra, to solve Process Synchronization problem in OS. Its most popular use is it solve Critical Section algorithm.

It uses signalling mechanism to allow access to shared resource namely by two –

  1. Wait
  2. Signal
Types Semaphore in OS Operating System

What is Semaphore in Operating System

Semaphore Types

There are two types of Semaphores –

  1. Binary Semaphore – Only True/False or 0/1 values
  2. Counting Semaphore – Non-negative value

Semaphore Implementation

Semaphore can have two different operations which are wait and signal. In some books wait signals are also denoted by P(s) and signal by V(s). Where s is a common semaphore.

Wait p(s) or wait(s)

  1. Wait decrements the value of semaphore by 1

Signal v(s) or signal(s)

  1. Signal increments the value of semaphore by 1


  1. Semaphore can only have non negative values
  2. Before start of program it is always initialised to 1
Binary Semaphore in OS Operating System
Counting Semaphore in OS Operating System

Code Logic for Incrementing – Decrementing value of Semaphore

Semaphore in OS Operating System

The code that needs to be protected can be surrounded by wait and signal operation as follows –

Semaphore Implementation OS Operating System

For Binary Semaphore

Let us try to understand the above code with an example –

  1. Imagine that there are two processes A and B.
  2. At the beginning the value of semaphore is initialised as 1. 
  3. Imagine process A wants to enter the critical section
    1. Before it can do that it checks the value of semaphore which is 1 thus, it can enter the CS and semaphore value is turned to 0
  4. Now imagine that process B wants to enter too
    1. It checks the semaphore value which is 0 thus it can’t enter and waits until the value is non zero – non negative value
  5. Now, Process A finishes and signals semaphore which in turns changes semaphore value to 1
  6. Thus, now process B can enter Critical section

In this way mutual exclusion was achieved.

For Counting Semaphore

For Counting Semaphore we initialise the value of semaphore as the number of concurrent access of critical sections we want to allow.

For example Let us assume that the value is 3.

  • Process 1 enters Critical section and semaphore value is changed to 2
  • Process 2 also enters critical section and semaphore value is changed to 1
  • Process 2 signals semaphore and comes out of critical section and Semaphore value is 2
  • Note at this moment only 1 process that is process 1 is in critical section
  • Process 3 and 4 also enter critical section simultaneously and semaphore value is 0
  • At this moment there are three processes in Critical section which are process Process 1, 3, 4
  • Now imagine that process 5 wants to enter the CS. It would not be able to enter as semaphore value is 0
  • It can only enter once any of the process 1, 3, 4 signals out of the critical section.

Thus value of semaphore in counting semaphore is equal to number of simultaneous processes allowed to access the critical section.


Monitors are a synchronisation construct that were created to overcome the problems caused by semaphores such as timing errors.

Semaphores can also be used but may cause timing errors. Mutex can never be used for process synchronisation

Mutex can not be used for the purpose of Process Synchronization

Operating System Process Synchornization

      Read More

    1. Process Synchronization
    2. Critical Section
    3. Inter-Process Communication
    4. UEFI(Unified Extensible Firmware Interface) and how is it different from BIOS
    5. Mutex
    6. Semaphore
    7. Mutex vs. Semaphore
    8. Atomic Operations in OS
    9. Peterson’s Algorithm for Mutual Exclusion (Only important for Cisco and Arista Networs)
      1. Java
      2. C
      3. Python
    10. Peterson’s Algorithm for Critical Section Problem (Only important for Cisco and Arista Networs)
    11. Readers-Writers Problem