Skip to content
Related Articles

Related Articles

Improve Article

Difference between Counting and Binary Semaphores

  • Difficulty Level : Medium
  • Last Updated : 29 Jun, 2021

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 :

Criteria

Binary Semaphore

Counting Semaphore

DefinitionA Binary Semaphore is a semaphore whose integer value range over 0 and 1.A counting semaphore is a semaphore that has multiple values of the counter. The value can range over an unrestricted domain.
Structure Implementationtypedef struct {
      int semaphore_variable;
}binary_semaphore;
typedef struct {      
        int semaphore_variable;
        Queue list;                                                   //A queue to store the list of task
      }counting_semaphore;
Representation0 means that a process or a thread is accessing the critical section, other process should wait for it to exit the critical section. 1 represents the critical section is free.The value can range from 0 to N, where N is the number of process or thread that has to enter the critical section. 
Mutual ExclusionYes, it guarantees mutual exclusion, since just one process or thread can enter the critical section at a time.No, it doesn’t guarantees mutual exclusion, since more than one process or thread can enter the critical section at a time.
Bounded waitNo, it doesn’t guarantees bounded wait, as only one process can enter the critical section, and there is no limit on how long the process can exist in the critical section, making another process to starve.Yes, it guarantees bounded wait, since it maintains a list of all the process or threads, using a queue, and each process or thread get a chance to enter the critical section once. So no question of starvation.

 

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :