What is semaphore in OS?

A semaphore in an operating system is a synchronization tool used to manage access to shared resources by multiple processes or threads. It helps prevent issues like race conditions, deadlocks, and resource contention in concurrent programming.

Real-World Example

Imagine a library with limited seating. A guard hands out tokens (semaphores) to students entering. If all tokens are taken, the guard stops others from entering until a token is returned. Similarly, a semaphore controls access to a resource by limiting the number of processes that can use it at a time.

Types of Semaphores

  1. Binary Semaphore

    • Can only have values 0 or 1.
    • Used for mutual exclusion, ensuring that only one process can access a resource at a time.
    • Example: Locking mechanisms for critical sections.
  2. Counting Semaphore

    • Can have a value ranging from 0 to a specified limit.
    • Used to control access to a resource with multiple instances (e.g., a pool of threads).
    • Example: Managing access to a database connection pool.

How Semaphores Work

  1. Initialization: A semaphore is initialized with a value representing the number of available resources.
  2. Wait (P Operation): A process decrements the semaphore's value before accessing the resource. If the value is zero, the process is blocked until a resource is available.
  3. Signal (V Operation): After a process finishes using the resource, it increments the semaphore's value, signaling other processes that a resource is free.

Advantages of Semaphores

  • Efficient Resource Sharing: Prevents multiple processes from accessing the same resource simultaneously.
  • Flexibility: Supports both mutual exclusion and synchronization for multi-threaded applications.
  • Fairness: Ensures orderly access to shared resources.

Disadvantages of Semaphores

  • Complexity: Incorrect usage can lead to programming errors like deadlocks or priority inversion.
  • Busy Waiting: Some semaphore implementations may cause CPU cycles to be wasted if processes continuously check for availability.
  • Overhead: Managing semaphores introduces computational overhead.

Applications of Semaphores

  1. Mutual Exclusion: Preventing concurrent processes from entering critical sections simultaneously.
  2. Synchronization: Coordinating processes to ensure proper execution order (e.g., producer-consumer problem).
  3. Resource Management: Allocating and deallocating resources like printers, disk drives, or memory.

Understanding semaphores is vital for designing robust multi-threaded systems. For more insights into concurrency and synchronization, explore Grokking Multithreading and Concurrency for Coding Interviews. Mastering semaphores will help you handle complex synchronization scenarios effectively.

TAGS
Coding Interview
System Design Interview
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
How to approach system scalability questions in interviews?
Is Netflix using Kafka?
What questions does Tesla ask in an interview?
Who owns Microsoft?
Why Microsoft is the best?
Top Concurrency and Multithreading Considerations for System Design Interviews
Learn the top concurrency and multithreading considerations for system design interviews. Explore best practices, real-world examples, and expert techniques for handling concurrent systems.
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.8
(1,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.