# Difference between Counting and Binary Semaphores

Last Updated : 03 Jan, 2023

Prerequisite: Semaphores in Process Synchronization

Overview

A semaphore is an integer variable that is useful for solving a variety of synchronization problems. It imposes deliberate constraints that help programmers avoid errors. Moreover, it makes the solution more organized, making the programs portable and efficient.  It can be accessed only through two standard atomic operations: wait() and signal(). An entity is the one that tries to access a shared resource. The entity can be a process or a thread. In this article, the term process and thread are changed interchangeably.

There are certain requirements, which guarantees synchronization, they are :

• Mutual Exclusion
• Progress
• Bounded wait

On the basis of a range of counter, a semaphore can be distinguished into two parts, one is the Counting Semaphore, while on the other hand, it is Binary Semaphore.

1. Binary Semaphore :

1. A Binary Semaphore is a semaphore whose integer value range over 0 and 1.
2. It is nothing, but similar to a lock, with two values: 0 and 1. Here 0 means busy, while 1 means free.
3. The idea behind using a binary semaphore is that, it allows only one process at a time to enter the critical section(thus allowing it to access the shared resource).
4. Here, 0 represents that a process or a thread is in the critical section(i.e. it is accessing the shared resource), while the other process or thread should wait for it to complete. On the other hand, 1 means that no process is accessing the shared resource, and the critical section is free.
5. It guarantees mutual exclusion since no two processes can be in the critical section at any point in time.
6. Since it is just a variable, which holds an integer value, it cannot guarantee bounded waiting. It might happen, that a process may never get a chance to enter the critical section, which may lead to its starvation. And we don’t want that.

2. Counting Semaphore :

1. A counting semaphore is a semaphore that has multiple values of the counter. The value can range over an unrestricted domain.
2. It is a structure, which comprises a variable, known as a semaphore variable that can take more than two values and a list of task or entity, which is nothing but the process or the thread.
3. The value of the semaphore variable is the number of process or thread that is to be allowed inside the critical section.
4. The value of the counting semaphore can range between 0 to N, where N is the number of the number of process which is free to enter and exit the critical section.
5. As mentioned, a counting semaphore can allow multiple processes or threads to access the critical section, hence mutual exclusion is not guaranteed.
6. Since multiple instances of process can access the shared resource at any time, counting semaphore guarantees bounded wait. Using such a semaphore, a process which enters the critical section has to wait for the other process to get inside the critical section, implying that no process will starve.

Using both the semaphore, a process is able to enter the critical section, so a progress is made.

Difference between Counting and Binary Semaphores :