hamburger

What is Semaphore in OS?

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Semaphore in OS is used to control the access of the shared device. Semaphore in the 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 semaphore in OS, 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). Task synchronization is also possible from Semaphore in OS. Let us discuss in detail the semaphore in OS with examples and its types in the upcoming sections.

Download Complete Operating System Formula Notes PDF

Download Formula Notes PDF for Compiler Design

What is Semaphore in OS?

The semaphore in OS (operating system) is initialized to the number of concurrent threads that you want to enter the section before they block.

Define Semaphore in OS

Logically, semaphore meaning in os says it is an integer variable that, 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 Semaphores 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 semaphores or mutexes

Which type of semaphore do we implement in os practical?

Binary

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 semaphores 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?

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 semaphores 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.

Important GATE Topics

And Gate Truth Table Man Full Form
JK Flip Flop Lan Full Form
JK Flip Flop Excitation Table Propped Cantilever Beam
Job Sequencing With Deadlines Torsional Force
Knapsack Problem Using Greedy Method Pop Full Form

Download Byjus Exam Prep App for the best Preparation

Online Classroom Program

BYJU’S Exam Prep Test Series

Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium