5

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?

2

4 Answers 4

8

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?

2
  • yes, so values greater than 1 for semaphores means the number of similar resources that can dispatch among processes and no blocking will happen among them. is it true? your answer was so helpful. thanks
    – muradin
    Commented Dec 19, 2013 at 11:30
  • Yes, that's the idea. Keep in mind that that scenario is pretty uncommmon - the vast majority of semaphore use is for mutual exclusion with the maximum resource count being 1. Commented Dec 19, 2013 at 15:17
8

Negative, there are exactly -N threads queued on the semaphore.

Zero, no waiting threads, a wait operation would put in queue the invoking thread.

Positive, no waiting threads, a wait operation would not put in queue the invoking thread.

enter image description here

The counting semaphores are usually used as guards of resources available in a discrete quantity. For example the counter may represent the number of used slots into a circular queue, producer threads would “signal” the semaphores when inserting items in the queue, consumer threads would “wait” for an item to appear in queue, this would ensure that no consumer would be able to fetch an item from the queue if there are no items available.

The maximum number is system dependent. The semaphore may be implemented using a file descriptor, in which case applications are able to open up at least a total of {OPEN_MAX} files and semaphores.

You can check the current value on your system by: cat /proc/sys/fs/file-max

1
  • I'm very appreciated to your conceptual answer. but Michael Burr answer is more technical and i should accept it. thanks.
    – muradin
    Commented Dec 19, 2013 at 11:22
1

A common way to thinki about semaphores is the analogy of a basket with balls. If there is a ball in the basket then the process or task can take the ball from the basket and get access to a shared resource or shared execution space.

enter image description here

Getting the ball from the basket represents counting down the semaphore and vice versa, putting a ball in the basket is an analogy for counting up the semaphore. It can be used for mutual exlusion, synchronization and other cases.

There usually is no specific limit to what values a semaphore can take besides that it's only integer values and restricted by the platform's implementation of integers.

It can depend on your specific use case whether the semaphore actually can reach a value > 1, in some cases it may and it others it's conceivable that the semaphore can't become larger than 1, but there the range will depend on the specific use of semaphores.

0

Its known as a counting semaphore. for eg :- you have a critical section of shared memory and you have initialized the counting semaphore to 10 this means that at a time only max 10 process can enter in the critical section for reading purpose. and for writing to critical section use binary semaphore, and use combination of both above mentioned and binary semaphore for accessing the region while reading.

for eg:- while writing decrement the binary semaphore counter to 0 and reading process will only check the binary semaphore counter if(0) wait if(1) ready to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.