Consider a computer game that is played as follows. You have G gold-makers each of which at random times produce one unit of gold and adds it to a data structure call the goldpile and D gold-diggers each of which can grab one unit of gold from the goldpile. The amount of gold that can be produced and available for digging can be arbitrarily large. Consider the following functions to be used in goldworld. Does it work correctly if run with multiple diggers and makers? Explain why or why not? If not, identify as many issues as you can. Focus on issues of how mutual exclusion is handled.
semaphore Y=1;
semaphore X=0;
void goldmaker()
{
while (true)
{
Wait a random amount of time.
Make one unit of gold.
semWait(Y);
Add the unit of gold to the goldpile;
semSignal(X);
semSignal(Y);
}
}
void golddigger()
{
While (true)
{
semWait(Y);
semWait(X);
take one unit of gold off the goldpile;
semSignal(X);
}
}