Operating System dining savages problem with monitor implementation: The problem
ID: 3674039 • Letter: O
Question
Operating System dining savages problem with monitor implementation: The problem of the dining savages (an allusion to the classic dining philosophers) is based on the arguably tasteless analogy of a number of members of a primitive culture, hereinafter called the "savages", sharing a meal from a single pot. The primary abstractions are the savages themselves, a cook, and the pot. The pot contains a certain number of servings of savage sustenance (the nature of which will be left to your imagination). Each of the savages can freely remove a serving from the pot so long as the pot is not empty. So before taking a serving, a savage must check to make sure the pot is not empty. In the case in which the pot is empty, the savage must wake up the cook to refill the pot, after which the feast continue. The savages, then, can eat only when the pot is not empty, and the cook can fill the pot only when the pot is empty. Question: The savage waits if there is EmptyPot, the cook waits for NotEmpty.
a. Can we, instead of two condition variables, use only one? (let’s say: potStatus). Discuss.
b. Is the assumption: If we have only one condition variable PotStatus, savage and cook cannot block at the same time on the condition variable correct?
Explanation / Answer
a)
Yes, this one variblle usage concept can be achieved through using monotors concept. Normally using semapahores will also solve race conditions or dead locks. But due to some prohrammatic errors some times they fail. So monitors is best choice. We will use one variable named pot and initialized with 0 (zero).
monitor diningSavagesProblem{
int pot;
function savage(){pot++;}
function cook(){pot--;}
int(){pot=0;}
}
Here as starting pot is full, so we call savage function by sending pot value = 1. So if pot is 1, then we can serve.
If pot is empty then it calls cook function , by decrementing pot variable, that is it sends arguement value 0. So if pot is zero then cook and finally called init() method where it triggers savage function again. So here we solved using one variable.
------------------------------------------------------------------------------------------------------------------------------------------------------
b)
Yes, as discussed in ablve questoin, we are relying on variable pot. So it cals sequentially and it too, it verifies the pot value whether 1 or 0. So only one thing will do and also sequentially. So there is no chance of block and wait endlessly (deadlock) at the same time.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.