We need value for x.wait and x.signal
We are working on 3 semaphores, mutex, x_sem, next .
Now, check the first code
x_count = x_count + 1;
So, x_count becomes 1
but next_count still 0
So, unable to enter in the first block x.wait
if (next_count > 0)
and E1 will not execute first
So, going to execute x.signal
here x.count >0 satisfying the condition
if block permit increment next_count to be 1
next_count = next_count + 1;
Now we want to execute x(ultimate we want to execute x)
So, V(x_sem) will be executed here
Wait(next) or P(next) will stop to execute code
Now , go to first part of code
if (next_count > 0)
V(next);
else
V(mutex);
------------------------------------------------------------ E1;
x_count = x_count - 1;
Now, next_count>0 is true.
So, code gets executed. V(next) and V(mutex) gets executed here. And also x_sem already executed. So, now,P(x_sem) will be executed for Mutual Exclusion.