wait(mutex);
...
body of function
...
if (next_count > 0) signal(next);
else signal(mutex);
I think I understand how semaphores work so I understand the wait(mutex) and signal(mutex) operations but despite a couple hours of reading different materials I don't understand the next_count or the signal(next). From my book:
Since a signaling process must wait until the resumed process either leaves or waits, an additional semaphore, next, is introduced, initialized to 0. The signaling processes can use next to suspend themselves. An integer variable next_count is also provided to count the number of processes suspended on next.
The resumed process refers to the one that initially called wait(mutex)? Why does the signaling process have to wait for the resumed process? The whole idea is that only one process at a time can execute the "body of function" code. Once a process calls signal(mutex) it is already out of that section of code, so why should it need to wait for the resumed process?