Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is general semaphores range?

What is the range of a general semaphore. I know that it can take negative values, 0 and 1. Negative values demonstrating the number of processes that are blocked in block queue. 0 means no process is in block and 1 means there is one resource available that no process have preempted it i want to know is it possible to have values greater than 1 for it.(2 for example) and what does it mean? Does it mean that we have more than one resource for a single semaphore?

like image 257
muradin Avatar asked Dec 06 '22 04:12

muradin


1 Answers

You should specify exactly what kind of semaphore you're talking about. Linux supports kernel semaphores, POSIX semaphores and System V semaphores.

The System V semaphore API documents that the semaphoer value cann't be less than 0.

The POSIX semaphore API documents that "If sem is locked, then the object to which sval points shall either be set to zero or to a negative number whose absolute value represents the number of processes waiting for the semaphore at some unspecified time during the call". It looks like the glibc implementation of POSIX semaphores doesn't allow the semaphore count/value to drop below zero.

Linux kernel semaphores used to have an implementation that tracked waiters using negative counts - this is the implementation that's documented in Bovet & Cesati's "Understanding the Linux Kernel" book. However at some point in the evolution of the 2.6 kernel (I think sometime after 2.6.11 and before 2.6.32) the implementation changed so that the semaphore value doesn't drop below zero.

So all semaphores allow a count greater than zero, which represents some number of resources that can be acquired at the same time. Whether a semaphore count can go below zero is an implementation detail - the behavior of those semaphores as far as waiting on a resource is the same as semaphore implementations that do not let the count drop below zero.

But the use cases where a semaphore count greater than 1 is useful is pretty rare. As Linus Torvalds said in a newsgroup posting (http://yarchive.net/comp/linux/semaphores.html):

However, almost all practical use of semaphores is a special case where the counter is initialized to 1, and where they are used as simple mutual exclusion with only one user allowed in the critical region. Such a semaphore is often called a "mutex" semaphore for MUTual EXclusion.

I've never really seen anybody use the more complex case of semaphores, although I do know of cases where it can be useful. For example, one use of a more complex semaphore is as a "throttle", where you do something like this:

/* Maximum concurrent users */    #define MAX_CONCURRENT_USERS 20
struct semaphore sem;

init_sema(&sem, MAX_CONCURRENT_USERS);

and then each user does a down() on the semaphore before starting an operation. It won't block until you have 20 users - you've not created a mutual exclusion, but you HAVE created a throttling mechanism. See?

like image 131
Michael Burr Avatar answered Dec 14 '22 17:12

Michael Burr