Reputation: 444
I was reading Silberschatz's book and semaphore that I saw somethings confusing .
There were two type of definitions for wait() in Silberschatz'book .
the first one is :
wait(S) {
while (S<= 0)
; //busy-wait
S--;
}
and the second one is :
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Which one is true ? when we call wait for semaphore , what we should do first ? checking semaphore value or decreasing amount of its value ?
Upvotes: 1
Views: 86
Reputation: 8292
Both are true but as mentioned in the book clearly that the first definition has problem of busy-waiting. That means it keeps the CPU busy but does no work and therefore wastes CPU cycles.
Whereas in the second definition there is no problem of busy waiting because of the fact that whenever the value of semaphore is not greater than 0, then the process is blocked and therefore enters the waiting state which means that it is taken off the CPU and can no more waste CPU cycles.
Upvotes: 2