3

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.
Mr. Sigma.
  • 1,301
  • 1
  • 16
  • 38

2 Answers2

4

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.

Ack.
  • 601
  • 1
  • 5
  • 9
0

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.

gnasher729
  • 32,238
  • 36
  • 56