Semaphores in Process Synchronization (original) (raw)

Last Updated : 15 Apr, 2026

A semaphore is a synchronization tool used in operating systems to manage access to shared resources in a multi-process or multi-threaded environment. It is an integer variable that controls process execution using atomic operations like wait() and signal(). Semaphores help prevent race conditions and ensure proper coordination between processes.

Working of Semaphores

A semaphore in OS uses two primary atomic operations:

1. wait(S)

1

Wait Operation

2. signal(S)

373547701

Signal Operation

**Example: Let’s consider two processes P1 and P2 sharing a semaphore S, initialized to 1:

This mechanism guarantees mutual exclusion, ensuring that only one process can access the shared resource at a time, see the image below for reference:

Semaphores

Features of Semaphores

Types of Semaphores

Semaphores are mainly of two Types:

1. Counting Semaphore

A counting semaphore can have values ranging from 0 to any positive integer. It is used when multiple instances of a resource are available and need to be managed.

2. Binary Semaphore

A binary semaphore has only two possible values: 0 and 1. It is mainly used for mutual exclusion, ensuring that only one process enters the critical section at a time.

Limitations of Semaphores