Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Counting Semaphores in Operating System

Counting semaphores refer to the synchronization mechanisms whose values range from [0,n], wherein n refers to a non-negative integer greater than one (1). Counting semaphores in this way can make numerous access tokens to a critical part available. As a result, many entities can simultaneously access the vital section.

In this article, we will look more into the Counting Semaphores in Operating Systems according to the GATE Syllabus for (Computer Science Engineering) CSE. Let us read ahead to find out more about it.

Table of Contents

What are Counting Semaphores in Operating Systems?

The semaphore variable is first initialized with the total number of resources available in counting semaphores. The wait() method is then executed anytime a process requires a resource, and the value of a semaphore variable gets decreased by one. The process then uses the resource, after which it calls the signal() function, which increases the value of a semaphore variable by one. When the value of this semaphore variable reaches 0, that is, when the process has utilised all of the resources and there are none left to be used, any other process that wishes to consume the resources must wait for its own turn. Then we establish the process synchronization in such a manner.

Illustration of Counting Semaphores in OS

A flowchart demonstrating a typical process of an entity accessing a vital region controlled by a counting semaphore is shown below:

Counting-Semaphores-in-Operating-Systems

Binary & counting semaphores perform comparable activities to synchronize access to a vital region, as can be seen. The main distinction is that counting semaphores verifies a range condition (in case any of the access tokens are available/not available). Whereas a binary semaphore tests a binary condition (in case the access is available/not available).

Using the wait operation or (P) in a binary or counting semaphore will produce the same result: the entity will wait in a queue until an access token is received. The signal operation or (V) in a binary semaphore that has a value 1 (one), on the other hand, has no effect. The signal operation in the very same conditions would increase the semaphore value into 2 (two) in a counting semaphore.

Where are Counting Semaphores Used?

There are situations where multiple processes must run in a key part at the same time. When more than one process is required in the crucial part at the same time, counting semaphores might be employed.

The semaphore implementation programming code is presented below, which comprises the structure of the semaphore and the logic that can be used to accomplish the entry and exit in the critical part.

Features of Counting Semaphore

  • A counting semaphore refers to a semaphore with several counter values. The value can span a wide range of possibilities.
  • It is a structure that consists of a variable known as a semaphore variable, which can accept more than two values, and a list of tasks or entities, which is nothing more than the process or thread.
  • The number of processes or threads authorised inside the crucial region is the value of the semaphore variable.
  • The counting semaphore’s value can range from 0 to N; wherein N is the number of processes that are free to enter or exit the crucial area.
  • As previously stated, several processes or threads can access the crucial region of a counting semaphore; hence mutual exclusion also isn’t guaranteed.
  • Counting semaphores assures bounded wait since numerous instances of the process can use the shared resource at the same time. A process that enters the crucial section must wait for the other process to enter the critical section using such a semaphore, suggesting that no process will starve.

Video on Counting Semaphore

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.

Also Explore,