Reputation: 1212
I have this example for which I'm trying to see what order the statements would run in. I believe it would be, X, A, B, Y.
Would I be right in saying so. My logic is statement X runs first in parallel with A, but when we arrive at the semaphore, it's still 0, thus we allow statement B to run due to the signal as this increments the value. Now we can finally run Y?
Upvotes: 0
Views: 741
Reputation: 1023
Because the processes run in parallel, the order of X and A cannot be determined - we could denote that as (X | A) - meaning that either of those is executed.
Now, for the semaphore: due to the processes being parallel, the order of actions is again (wait (consyn) | signal (consyn), where different outcomes
If wait (consyn) is executed first and signal (consyn) after that, then the resulting scenario is (X | A) (Y | B) with P1 and P2 ending. If the order is reversed and semaphore implementation uses signal to increment it's value and wait to decrement, the outcome is again (X | A) (Y | B) with P1 and P2 ending.
Additionally, the order of thread execution doesn't have to be perfectly parallel, and therefore the result can be anything where order of (X, Y), (A, B) and (A, Y - the guarantee of the semaphore) is preserved.
Upvotes: 2
Reputation: 52622
There is very little that you can say about the order of statements. You have the order X / wait / Y. You have the order A / signal / B. And you have the order signal / Y. That's all. If you are not interested in the wait / signal which are just tools, then Y executes after A and that is all you know.
P2 can run completely to its end before the first statement of P1 is executed.
Upvotes: 2