written 3.0 years ago by |
Binary semaphore
The value of a binary semaphore can range only between 0 and 1. Thus, binary semaphores behave similarly to mutex locks. In fact, on systems that do not provide mutex locks, binary semaphores can be used instead for providing mutual exclusion.
following is the code of signal() operation in semaphore with no busy waiting (without busy waiting)
Implementation of signal():
signal (semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
The fact that S->value is zero or negative means that there is no available resource, so wakeup() should not be permitted. but as you can see, whenever signal() operation is called, a process (which is in waiting list) is being woken regardless of the state of S->value.
A sign of inequality S->value >= 0 is natural and makes sense, because S->value > 0 means there are available resources.