Skip to content
The Computer Science
TheCScience
  • Engineering Subjects
    • Human Values
    • Computer System Architecture
    • Microprocessor
    • Digital Communication
    • Internet of Things
  • NCERT Solutions
    • Class 12
    • Class 11
  • Solutions
    • HackerRank
      • C Solutions
      • C++ Solutions
      • Java Solutions
      • Python Solutions
      • Algorithms Solutions
      • Data Structures Solutions
    • HackerEarth Solutions
    • Leetcode Solutions
  • JEE 2027
The Computer Science
TheCScience

Semaphores in Operating Systems | OS Tutorials

YASH PAL, June 2, 2026June 2, 2026

To solve the critical section problem in an operating system, we have some hardware-based approaches that we use to achieve mutual exclusion in an operating system, but these approaches are complicated for application programmers to use while developing real programs for an operating system. To overcome this difficulty, we use a synchronization tool called a semaphore.

Table of Contents

  • Semaphore in an Operating System
    • Usage of Semaphore
    • Binary Semaphore
    • Semaphore Properties
    • Drawback of Semaphore

Semaphore in an Operating System

A semaphore “S” is an integer variable that, apart from initialization, is accessed only through two standard atomic operations:

  1. Wait()
  2. Signal()

The wait() operation was originally termed P, and signal() was originally called V.

Definition of Wait (P):

Decrements the value of the semaphore variable “S” as soon as it becomes positive.

Wait (s){
   While (S <= 0);  //no-op
   S--;
}

Definition of Signal (V):

Increments the value of the semaphore variable “S” by 1.

Signal(s){
S++;
}

Modification of the integer variable “S” in both WAIT and SIGNAL operations is indivisible.

Indivisible:

  • It means when one process modifies the value of the semaphore variable “S”, no other process can simultaneously modify that same semaphore value.
  • In addition, in the case of wait(S), the testing of the integer value of S (S <= 0), as well as its possible modification (S–), must be executed without interruption.

Usage of Semaphore

Operating systems often distinguish between counting and binary semaphores. The value of a counting semaphore can range over an unrestricted domain.

Binary Semaphore

  • A binary semaphore is a semaphore with an integer value that can range only between 0 and 1. In principle, it should be easier to implement the binary semaphore.
  • In this, a queue is used to hold processes waiting on the semaphore. The process that has been blocked the longest is released from the queue.
  • Semaphores are not provided by hardware. But they have several attractive properties.

Semaphore Properties

  • Machine-independent – no need for code at the assembly level as in test-and-set.
  • Simple.
  • Works with any number of processes.
  • Can have different semaphores for different critical sections.
  • A process can acquire multiple needed resources by executing multiple downs.
  • Can allow just one process into the critical region using a simple binary semaphore, or more than one if desired using counting semaphores. (NB: Unix supports arrays of counting semaphores – more on that next week)
  • For both semaphores and binary semaphores, a queue is used to hold processes waiting on the semaphore. FIFO policy is used to remove the processes from the queue.
  • The process that has been blocked the longest is released from the queue first is called a strong semaphore. A semaphore that does not specify the order in which processes are removed from the queue is called a weak semaphore.

Drawback of Semaphore

  1. They are essentially shared global variables.
  2. Access to semaphores can come from anywhere in a program.
  3. There is no control or guarantee of proper usage.
  4. There is no linguistic connection between the semaphore and the data to which the semaphore controls access.
  5. They serve two purposes: mutual exclusion and scheduling constraints.
engineering subjects Operating System Operating System

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Engineering Core Subjects

Digital Communication Subject
Internet of Things Subject
Computer Architecture subject
Human Value Subject

JEE Study Materials

JEE Physics Notes
JEE Chemistry Notes

TheCScience

At TheCScience.com, our mission is to make quality education accessible to everyone. We provide in-depth, easy-to-understand articles covering Secondary, Senior Secondary, and Graduation-level subjects.

Pages

About US

Contact US

Privacy Policy

DMCA

Our Tools

Hosting - get 20% off

Engineering Subjects

Internet of Things

Human Values

Digital Communication

Computer System Architecture

Microprocessor

Programming Tutorials

Data Structure and Algorithm

C

Java

NCERT

Class 12th

©2026 TheCScience | WordPress Theme by SuperbThemes