Semaphore in Operating System (OS) (What is)

Semaphore in Operating System

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

Lock

Semaphore in Operating System

Semaphore uses signalling mechanism to allow access to shared resources namely by two –

  1. Wait
  2. Signal

We recommend you to go through this article below first –

What is Mutex Lock in 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

Semaphore

  1. Semaphore can only have non-negative values
  2. Before the start of the program, it is always initialised to 
    1. n in Counting semaphore (Where n is the number of processes allowed to enter critical section simultaneously)
    2. 1 in the case of a binary semaphore
Semaphore In Operating System newer

Signal Operations

  • Signal operation is simple
  • It increases the value of semaphore by 1 as shown below
signal(S)
{
    S++;
}

Wait Operations

  • Wait operation decrements the value of semaphore S if S is a positive number
  • Else if S is 0 or negative then code gets stuck at while loop as it keeps implementing infinitively
  • The semi-colon after while forces while loop definitively if S is 0 or negative
  • Thus the code doesn’t move ahead in hopes that the value of S will increase because of some other signal operation elsewhere

Code Logic for Incrementing – Decrementing value of Semaphore –

wait(S)
{
    while (S<=0);

   S--;
}

Actual Working for both functions together to achieve access of critical section –

    // some code

    wait(s);


    // critical section code


    signal(s);


    // remainder code

The eventual goal is to protect the critical section code using wait and signal operations.

You can visualize the whole operation on how the Semaphore system works with the example below

Semaphore in Operating System 3

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.

Semaphore in Operating System 4

Monitors

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