# Increment and Decrement on binary semaphores when their values are $1$ and $0$ respectively?

What happens if we increment binary semaphore $$x$$ when it is equal to $$1$$. E.g A process executes $$V(x)$$ when $$x==1$$; What are the possible cases then-

• Process would be blocked till $$x$$ becomes $$0$$. The moment $$x$$ becomes $$0$$,it is incremented.

• Process would be blocked and no present or future changes will be there for $$x$$. i.e Process assumes as if there was never a $$V(x)$$.

• Other. (If there is please mention).

Same query for $$P(x)$$ operation as well when $$x=0$$.

• Actually, Galvin doesn't mention in pseudo code what to do during such condition.
• Better if Someone could provide standard code for $$P(x)$$ and $$V(x)$$... Also, $$P$$ is wait operation whereas $$V$$ is signal operation.

A binary semaphore has the wait() and the signal() method. The one which causes a process to stop is the wait() method, while the one that increments the counter x in the semaphore is signal(). If signal is called when x=1, then the call is just ignored because the queue of blocked processes is empty, and x has already its maximum value. The process that called signal() doesn't block.

• Oh. Got it 2nd case without any blocking. V(x) would be ignored. – Mr. Sigma. Jan 19 '17 at 16:05
• Can you please share some source? – Mr. Sigma. Jan 9 '19 at 14:08
• I had opened a bounty to get more on this. But it seems no one is updating. Finally giving you. – Mr. Sigma. Jan 16 '19 at 8:26

Actually, a semaphore doesn't ignore signal calls. A signal call counteracts a wait call - even if the signal happens before the wait call.

The typical sequence is: Create a semaphore S. Start an action on a different thread that will eventually signal S. Do some work or not. Wait on the semaphore. After the wait call returns, the action on the other thread has succeeded.

We don’t know how long it takes the other thread to perform the action and get to the Signal call, and it mustn’t matter. So if the Signal happens before the Wait, it is not just ignored. The semaphore records it, and when the Wait call happens, it returns immediately without stopping. So after the Wait return, the action has finished.

It Must work this way, otherwise the behaviour if the Signsl happens a nanosecond before or after the Wait call would be completely different.

• Thanks for answer. Do you mean with the first case? Can you please explain. – Mr. Sigma. Jan 21 '17 at 4:32