The easiest way to do this is semaphores . Here's how the semaphore works:
A semaphore is basically a variable that takes zero / positive values. Processes can interact with it in two ways: increase or decrease the semaphore.
Increasing the semaphore adds 1 to this magic variable , and more about that. This reduces the number of interesting things : if the counter reaches zero, and the process tries to lower it again, since it cannot accept negative values, it will be blocked until the variable increases .
If several process blocks expect a decrease in the semaphore value, only one wakes up for each unit, the count increases.
This greatly simplifies the creation of a work / target system: your manager processes tasks and increases the value of the semaphore in accordance with other elements, and your work processes try to reduce the score and constantly get the task. When tasks are not available, they block and do not consume CPU time. When one appears, only one of the inactive processes will wake up. The magic of Insta-sync.
Unfortunately, at least in the Unix world, the semaphore API is not very friendly, because for some reason it considers sempahores arrays rather than separate ones. But you are a simple shell from a nice interface!
Hooray!
slezica
source share