Semaphore in Operating System (OS)

Semaphore in Operating System

Semaphore in operating system  is  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

Signal Operations

  • Signal operation is simple
  • It increases the value of semaphore by 1 as shown below

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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription