What is a semaphore?

A semaphore in computer science is a synchronization tool used to control access to a common resource by multiple processes in a concurrent system such as a multitasking operating system. It helps in preventing race conditions when multiple threads or processes need to access shared resources.

Basic Concept:

  • Semaphore: It's like a counter combined with a set of rules for how that counter is allowed to be changed. It's used to signal between processes or threads.

Types of Semaphores:

  1. Binary Semaphore (or Mutex):

    • Acts as a lock, with only two states: locked (0) or unlocked (1).
    • It's used to ensure that only one thread or process can access the critical section at a time.
  2. Counting Semaphore:

    • Has a non-negative integer value and is used to control access to a resource that can support multiple instances.
    • It's used for managing a fixed number of identical resources.

How Semaphores Work:

  • Wait Operation (P):
    • Before entering the critical section, a process or thread executes the wait operation. If the semaphore's value is greater than zero, it's decremented and the process proceeds. If it's zero, the process is blocked until the semaphore is available.
  • Signal Operation (V):
    • After leaving the critical section, the process or thread executes the signal operation, incrementing the semaphore's value. If other processes are waiting, one of them gets unblocked.

Example:

Imagine a scenario where you have a limited number of printers (say, 3) shared among several processes:

  • You would use a counting semaphore initialized to 3 (the number of printers).
  • When a process needs to use a printer, it performs a wait operation. If a printer is available (semaphore value > 0), the semaphore is decremented, and the process uses the printer.
  • When the process finishes using the printer, it performs a signal operation, incrementing the semaphore and potentially unblocking a waiting process.

Importance in Programming:

  • Resource Management: Semaphores are crucial in managing access to limited resources in concurrent programming.
  • Avoiding Deadlocks and Race Conditions: Proper use of semaphores can prevent deadlock scenarios where two or more processes are waiting indefinitely for resources held by each other, and race conditions where the outcome depends on the sequence of execution.

In summary, semaphores are fundamental in the realm of concurrent programming, providing a mechanism to avoid conflict and ensure correct program execution when multiple threads or processes need to interact with shared resources.

TAGS
Dynamic Programming Patterns
Data Structures and Algorithms
Coding Interview Questions
FAANG
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are the common algorithms for LeetCode?
What is the purpose of the system design interview?
What is Sliding Window coding pattern?
Can I learn Python using LeetCode?
What is the longest palindromic substring approach?
What is the difference between Array and ArrayList in Java?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.