Semaphore in Operating System (OS)

Semaphore in Operating System

Semaphore in Operating System (OS) is an entity devised by Edsger W. Dijkstra to solve the Process Synchronization problem in OS. Its most popular use is to solve the Critical Section problem, where multiple processes need access to shared resources without interfering with each other. Semaphores act as signaling mechanisms that help manage concurrent processes by using simple integer variables.

These variables can be incremented or decremented using operations like wait and signal.

Lock

Semaphore in Operating System

A Semaphore is a synchronization tool used in operating systems to control access to a shared resource by multiple processes. It is a type of signaling mechanism that helps prevent race conditions, ensuring that only one process can access a critical section at a time.

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

  1. Wait
  2. Signal

What is Semaphore in Operating System

A Semaphore is a synchronization tool used in operating systems to manage access to shared resources by multiple processes in a concurrent system. It serves as a signaling mechanism to control the execution order of processes and ensure mutual exclusion, preventing race conditions and deadlocks. The concept of semaphores was introduced by Edsger Dijkstra.

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

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

Ending statement

semaphores play a crucial role in operating systems by ensuring proper synchronization between processes, particularly when managing access to shared resources. Introduced by Edsger W. Dijkstra, semaphores prevent issues like race conditions and deadlocks. They come in two types: binary semaphores and counting semaphores, each serving different purposes depending on the system’s needs. By using simple operations like wait and signal, semaphores control process execution and enable mutual exclusion in critical sections. This synchronization mechanism is essential for maintaining system stability and efficiency in concurrent computing environments.

FAQs

The concept of semaphores was introduced by Edsger W. Dijkstra to address synchronization issues in concurrent programming.

Yes, improper use of semaphores like failing to release them can lead to deadlocks where processes wait indefinitely for each other.

A mutex allows only one thread access to a resource at a time, while a semaphore can allow multiple accesses, depending on its value.

No, semaphores are also widely used in multithreaded applications and embedded systems to handle resource sharing and synchronization.