0
votes

I am using System V semaphores and want to block a process at its start and wait for another process to up the specific semaphore after it has completed its critical section.
I think that sem_op = 0, would be the proper action to do.

So I initialize the semaphore with 1.
Then at process A, that needs to be blocked I am using the sem_op = 0 in order to wait for the semaphore to became 0. At process B I am using sem_op = -1. (So this makes the semaphore.val = 0).

And the process that should be blocked prints at stdout when the other has succesfully down'ed the semaphore.

What does the wait for zero functionality actually do and how can I properly use it?

Thank you for your time!

1

1 Answers

0
votes

As per the manual, semop = 0 is the "wait-for-zero" operation. So your algorithm should work. However, it is preferable to solve these problems using the traditional P and V operators, where P means semop = -1 and V means semop = +1. So if the initial value of semaphore is 0, Process A should do a P operation and block. Process B should complete its critical section and do a V operation. As soon as Process B does a V operation, Process A's P operation would be completed and it can progress ahead.