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.
  • $\begingroup$ Neither of these, the fact that the max value of a binary-semaphore is 1 is simply a precondition/invariant that must be maintained. Asking what happens if you violate it is like asking what happens if you try to move an immovable object. In real-world implementations, it's "undefined behavior" could throw an exception, could just silently fail, etc. $\endgroup$
    – 1110101001
    Sep 21, 2023 at 4:47
  • $\begingroup$ See stackoverflow.com/questions/20454097/… or stackoverflow.com/questions/20141342/… $\endgroup$
    – 1110101001
    Sep 21, 2023 at 4:49

2 Answers 2


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.

  • $\begingroup$ Oh. Got it 2nd case without any blocking. V(x) would be ignored. $\endgroup$
    – Mr. Sigma.
    Jan 19, 2017 at 16:05
  • $\begingroup$ Can you please share some source? $\endgroup$
    – Mr. Sigma.
    Jan 9, 2019 at 14:08
  • $\begingroup$ I had opened a bounty to get more on this. But it seems no one is updating. Finally giving you. $\endgroup$
    – Mr. Sigma.
    Jan 16, 2019 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.

  • $\begingroup$ Thanks for answer. Do you mean with the first case? Can you please explain. $\endgroup$
    – Mr. Sigma.
    Jan 21, 2017 at 4:32

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.