Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Counting and Binary Semaphores

What is the difference between Counting and binary semaphore.

What I have seen somewhere is that both can control N number of processes which have requested for a resource. Both have taken and Free states.

Is there any restriction on how many Resources a Binary semaphore and Counting semaphore can protect?

Both allow only One process to use a resource at a time...

Is there any other difference? Are the above mentioned properties correct?

like image 487
Umer Farooq Avatar asked Jun 05 '12 13:06

Umer Farooq


People also ask

What is the difference between binary and counting semaphores?

A 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.

Why you use binary semaphores instead of counting semaphores?

A binary semaphore is restricted to values of zero or one, while a counting semaphore can assume any nonnegative integer value. A binary semaphore can be used to control access to a single resource. In particular, it can be used to enforce mutual exclusion for a critical section in user code.

What are semaphores explain binary and counting semaphore with an example?

A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1) Wait, and 2) Signal for the process synchronization. A semaphore either allows or disallows access to the resource, which depends on how it is set up.

What is semaphore and counting semaphores?

Semaphores are typically used to coordinate access to resources, with the semaphore count initialized to the number of free resources. Threads then atomically increment the count when resources are added and atomically decrement the count when resources are removed.


1 Answers

Actually, both types are used to synchronize access to a shared resource, whether the entity which is trying to access is a process or even a thread.

The difference is as follows:

Binary semaphores are binary, they can have two values only; one to represent that a process/thread is in the critical section(code that access the shared resource) and others should wait, the other indicating the critical section is free.

On the other hand, counting semaphores take more than two values, they can have any value you want. The max value X they take allows X process/threads to access the shared resource simultaneously.

For further information, take a look at this link.
http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:semaphores_mutexes

EDIT
The max value that a counting semaphore can take is the the number of processes you want to allow into the critical section at the same time.
Again, you might have a case where you want exclusion over a certain resource, yet you know this resource can be accessed by a max number of processes (say X), so you set a counting semaphore with the value X.

This would allow the X processes to access that resource at the same time; yet the process X+1 would have to wait till one of the processes in the critical section gets out.

like image 126
Fingolfin Avatar answered Sep 25 '22 20:09

Fingolfin