Parallel programming - Semaphore (Counter Variable)

Data System Architecture


A semaphore is a variable that control the access (physical or logical) by multiple thread to a common resource.

A semaphore is a data structure that is initialized to a positive integer value and that can be locked multiple times. As long as the semaphore value is positive, locking it will return the current value and the locking process will continue execution immediately; the semaphore will be decremented upon locking. Releasing the lock will increment the semaphore again.

Once the semaphore has reached zero, the next process that attempts to acquire a lock will be suspended until another process releases its lock and this increments the semaphore again.

They are used for synchronization between two or more processes.

Example / Usage


If the stoplight is green, a train can enter the train station. If it is yellow or red (or any other color), the train station cannot be accessed.

Connection Pool

To implement a connection pool.

When an application supports maximum 10 connection, a semaphore with the value 10 will be created.

  • Whenever the application creates a connection, the semaphore will be decremented by 1.
  • Whenever the application disconnect a connection, the semaphore will be incremented by 1.
  • When the semaphore is above 10, the application will wait or return a message.

Process Resource Limitation

With a semaphore, you limit the resources for a process such as the number of Open File, CPU, .. for:


Documentation / Reference

Discover More
Data System Architecture
Concurrency - Latches (System Lock)

Latches are like semaphores. Latches are used to guarantee physical consistency of data, while locks are used to assure logical consistency of data. Latches are simple, low-level system lock (serialization...
Data System Architecture
Concurrency - Lock (Mutex)

A lock is a synchronizationmechanism designed to enforce a mutual exclusion of threads. A lock is also known as a mutex. Type: binary semaphore - yes / no Most locking designs block the execution...
Linux - Kernel Semaphore parameters

On Linux, A semaphore is a System V IPC object that is used to control utilization of a particular process. Semaphores are ashareable resource that take on a non-negative integer value. They are manipulated...
Data System Architecture
Parallel Programming - Producer Consumer problem (Bounded-buffer problem)

The producer–consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem in a producer consumer mode. The problem describes two processes...
Process States
Process - Inter Process Communication (IPC)

An Inter Process Communication (IPC) describes the fact that two or more processes exchange information. IPC is used not just for communication between processes on the same system, but also on different...

Share this page:
Follow us:
Task Runner