Bytes
Web Development

Semaphore in Operating System

Last Updated: 17th June, 2025
icon

Jay Abhani

Senior Web Development Instructor at almaBetter

Master Bootstrap with this quick-reference cheat sheet. Includes essential classes, components, and layout utilities for responsive design

Semaphore in Operating System

Concurrency is very important in modern operating systems. With the rise of multiprocessor systems as well as parallel execution one of the fundamental tools to maintain the synchronization of processes has become the semaphore. Whether you're learning about OS concepts, trying to prepare for a job interview, or writing concurrent programs in Java you'll need to know about the semaphores in OS.

This article deals with what is semaphore in OS, types, usage (different to mutexes compared) and actual implementations.

What is Semaphore in OS?

A semaphore in operating system is a synchronization primitive that manages access to shared resources by multiple processes in a concurrent system. It is basically a shared integer variable that allows access to be controlled by enforcing constraints on its current value.

The term " semaphore " comes from signalling mechanisms in railways and ships, and in computing is very much like that: it 's a signal to coordinate activities among processes.

Definition

A semaphore is a shared integer variable that can be manipulated using two atomic operations: wait(*v) and signal(*p) (in some literature these are P(3) and V(4)). They are used to manage availability of resources.

Why is Semaphore Used?

In a multiprogramming environment there may be many processes that want to access the same resource ( such as a printer or a file ), and as long as there is no control they can cause race conditions, inconsistent data or even deadlock.

Semaphores are used to:

Avoid race conditions

Provide mutual exclusion

Ensure synchronization between processes

Coordinate resource sharing safely

How Semaphores Work

semaphore in OS uses two primary atomic operations:

  1. wait(S)
wait(S) {
    while(S <= 0); // busy wai
    S = 
}

If the semaphore value is positive, it decrements the value and continues.

If it is zero or negative, the process waits until it becomes positive.

2. signal(S)

signal(S) {
    S = S + 1;
}

Increments the value of the semaphore.

Wakes up a waiting process if there is one.

Types of Semaphore in OS

There are two main types of semaphores in OS:

1. Binary Semaphore in OS

Can only take two values: 0 and 1.

Used primarily for mutual exclusion.

Acts like a mutex.

2. Counting Semaphore in OS

Can take any non-negative integer value.

Used to control access to a resource that has a limited number of instances.

Commonly used in resource pooling (e.g., a database connection pool).

Semaphore vs Mutex

A common confusion arises between semaphores and mutexes. Here's a breakdown:

FeatureSemaphoreMutex
OwnershipNot owned by any thread/processOwned by the thread that locks it
Value RangeInteger (can be >1)Binary (only locked/unlocked)
TypesBinary, CountingOnly one type
Use CaseSignaling and resource managementMutual exclusion only
BlockingMay or may not block (based on logic)Automatically blocks if not free

mutex vs semaphore: Use mutex when only one thread can access a resource; use semaphore for signaling and resource limits.

Use Cases of Semaphores in Operating System

1. Process Synchronization

Semaphores coordinate processes that have to happen in a certain order. You could use it to make sure a producer writes to a buffer before a consumer reads it.

2. Mutual Exclusion

Binary semaphores work like locks, only one process can see a particular section at a time.

3. Managing Limited Resources

This is to control access to resources like database connections where the total number of semaphores is fixed.

Semaphore in Java

Java provides built-in support for semaphores via the java.util.concurrent.Semaphore class. Here's an example of a counting semaphore:

import java.util.concurrent.Semaphore;

public class SharedResource {
    static Semaphore semaphore = new Semaphore(3); // allow 3 permits

    static class Worker extends Thread {
        public void run() {
            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName() + " acquired a permit.");
                Thread.sleep(1000); // simulate work
                System.out.println(Thread.currentThread().getName() + " releasing permit.");
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 6; i++) {
            new Worker().start();
        }
    }
}

Output:

Thread-0 acquired a permit.
Thread-1 acquired a permit.
Thread-2 acquired a permit.
...

This example simulates 6 threads competing for 3 permits—a real-world usage of counting semaphores.

Limitations of Semaphores

Despite their usefulness, semaphores have certain downsides:

Busy waiting (in spinlocks or naive implementation)

Can lead to priority inversion

Improper use can result in deadlocks or resource starvation

Hence, semaphores must be used carefully and correctly.

Advantages of Semaphores in OS

Efficiently handles multiple resource management

Enables inter-process communication

Helps in avoiding race conditions

Supports multiple process synchronization

Best Practices for Using Semaphores

Always pair wait() with signal() to avoid deadlock

Prefer higher-level abstractions like mutexes or monitors when available

Use binary semaphores for mutual exclusion, counting semaphores for resource pools

Conclusion

Semaphores in operating system A semaphores is an important part of the OS whereby processes and threads can be safely and synchronized execution. If you are working with concurrent system like C++, threads in Java or multi-threaded system you must understand about types of semaphores and how they are implemented and the use cases.

For mutual exclusion in OS use binary semaphores and for resource sharing use counting semaphores You can develop stable and race-free applications using binary semaphore Remember semaphore is shared integer variable that makes synchronization in many modern computing system

Frequently asked Questions

What are two types of semaphores?

Binary semaphore: Can be 0 or 1; used for mutual exclusion.
Counting semaphore: Integer value ≥ 0; used to control access to a set of identical resources.

What is semaphore and mutex?

A semaphore is a signaling mechanism; it can be used to control access by multiple processes.
A mutex (mutual exclusion) is a locking mechanism where only one thread can access the resource

Why is semaphore used?

Semaphores are used for:
Synchronizing concurrent processes
Managing shared resources
Preventing race conditions and ensuring order of execution

When to use a semaphore?

When multiple resources of the same kind are shared (counting semaphore)
When process coordination or order is needed (binary semaphore)
When you want a signaling mechanism (e.g., producer-consumer problems)

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2025 AlmaBetter