What is Semaphore in OS? | Semaphore in Operating System

By Priyanshu Vaish|Updated : June 10th, 2022

Semaphore in OS (operating system) is used to control the access of the shared device. Semaphore in operating system is machine-independent and simple to implement. From this, we can easily determine the correctness of the code or program in which the semaphore variable is used.

From different semaphore in OS (operating system), we can have many different critical sections and acquire many resources at the same time. Due to the busy waiting, there is no waste of resources in semaphore in OS (operating system). The task synchronization is also possible from Semaphore in OS. Let us discuss in detail the semaphore in OS and its types in the upcoming sections.

Table of Content

What is Semaphore in OS (Operating System)?

The semaphore in OS (operating system) is initialized to the number of concurrent threads that you want to enter the section before they block. Logically, a semaphore in OS (operating system) is an integer variable that is, apart from the initialization of the semaphore, can be only accessed through the mutually exclusive two atomic operations that are:

  • Wait()
  • Signal().

Wait(S) or P(S)

If the semaphore value for a certain process is smaller than 0, the process goes into a block state or is added to the waiting queue. If the semaphore value for a certain process is greater than 0, then the semaphore value is decremented. The structure of the atomic operation wait() is as below:

wait(S)

{

    while(S≤ 0);

     S = S – 1;

 }

Signal(S) or V(S)

The atomic operation signal(S) or V(S) increments the semaphore value. The structure of the atomic operation signal() is as below:

signal(S)

{

    S = S + 1;

 }

Types of Semaphore in OS

A semaphore is essentially a non-negative integer that acts as a signal to address the critical section problem. It is a concept in operating systems that allows concurrent processes to be synchronized. The semaphore in OS(operating system) is categorized into two different types that are as follows:

  • Counting semaphore
  • Binary semaphore or mutexes

What is Counting Semaphore in OS?

The counting semaphore in OS has two elements: an integer value and an associated waiting list (mostly a queue). The value of the counting semaphore in OS (operating system) may be positive or negative.

The operation of counting semaphore in OS(operating system) includes:

  • Wait (also referred to as Down operation, P operation).
  • Signal (also referred to as Up operation, V operation, or Release operation).

What is Binary Semaphore in OS (Operating System)?

A binary semaphore in OS has two elements: the integer values can be 0 or 1, and an associated waiting list (mostly a queue). The waiting list(queue) of binary semaphores in OS indicates the blocked processes when trying to enter the critical section. The wait operation is executed when a process tries to enter the critical section. Some of the possible cases with the value of binary semaphore are as follows.

Case I: Binary semaphore value = 1

Suppose the binary semaphore value is 1. The value of the binary semaphore is set to 0 if any process is allowed to enter the critical section.

Case II: Binary semaphore value = 0

Suppose the binary semaphore value is 0. No process is allowed to enter into the critical section of the code and is blocked and kept in the waiting queue. The signal operation is executed when a process takes an exit from the critical section.

Important Points for Semaphore in OS (Operating System)

Every semaphore variable will have its own suspended list. Let us check the important points for semaphore in OS given below.

  • Down and Up operations are atomic that is, no preemption can occur in between, and thus, interrupts are disabled.
  • If more than one process is on the suspended list, then whenever we perform one up operation, one process will wake up from the suspended list, which will be based on the FIFO order.
  • If two or more processes are on the suspended list and there is no other process to wake up these processes, then those processes are said to be involved in the deadlock.
  • When the processes are on the suspended list, they remain in the ready state but will not participate in the scheduling.

Advantages of Semaphore in OS

Semaphores are implemented in the microkernel's machine-independent code. As a result, they are machine-agnostic. Let us check some more advantages of semaphore in OS.

  • Only one process is allowed to enter the critical part thanks to semaphores. They closely adhere to the mutual exclusion principle and are far more efficient than other synchronization methods.
  • Because processor time is not wasted unnecessarily checking if a condition is met to allow a process to access the vital region, there is no resource waste due to busy waiting in semaphores.

Comments

write a comment

FAQs on Semaphore in OS

  • The semaphore in OS(operating system) is used to signal from one task to another. The mutex semaphore is meant for the resource that can be released and taken in the order in which each task uses the shared resources.

  • The main function of a semaphore in OS(operating system)is that it is used to provide a synchronization mechanism which is similar to a machine interface (MI) lock or a mutex. The semaphore in OS(operating system) is also used to notify other threads of resource availability or control access to shared resources.

  • The semaphore in OS(operating system) can be implemented within the operating system by interfacing with the scheduling queues and process state. The thread blocked by the semaphore in OS(operating system) will be moved from a running state to a waiting state(a semaphore specific waiting queue).

  • The priority inversion is the limitation of semaphore in OS(operating system), which is the big limitation of semaphore in OS(operating system). The use of semaphore in OS(operating system) is not enforced but is used by convention only.

  • The code of the wait() and signal() operation of semaphore in os(operating system) are as follows:

    wait(S)

    {

       while(S≤ 0);

        S = S – 1;

     }

    signal(S)

    {

       S = S + 1;

     }

Follow us for latest updates