[Points 3] What is the purpose of a condition variable in a monitor? How is the
ID: 3847906 • Letter: #
Question
[Points 3] What is the purpose of a condition variable in a monitor? How is the condition variable implemented? Explain your answer using an example.
[Points 3] In the monitor solution to the dining-philosopher’s problem (Figure 5.18), what happens when a process i calls test(i). In other words, what is being done in test(i)?
Q2.
Answer the following in the context of semaphores.
[Points 3] Write a piece of code to implement a solution for critical section problem using semaphores (with wait and signal operations). (Hint: similar to Peterson’s solution but using semaphores)
[Points 3] What are the advantages (if any) of using the above solution with semaphores instead of the one proposed by Peterson.
228 Chapter 5 Process Synchronization monitor DiningPhilosophers enum THINKING, HUNGRY, EATING) state C5]; condition self [5]; void pickup (int i) f state Cil HUNGRY tests (i); if (state [i] EATING) self [i] wait(); void putdown (int i) state [i] THINKING test (Ci 4) 5); tests ((i 1) 5); void test (int i) if ((state CCi 4) 5] EATING) && (state [i] HUNGRY) && (state (i 1) 5] EATING)) state [i] EATING self [i] signal); initialization code for (int i 0; i 5; i++) state[i] THINKING Figure 5.18 Amonitor solution to the dining-philosopher problem.Explanation / Answer
I am answering your first 2 questions, as they ae related.. I request you to please post your other questions in a seperate thread as due to time constraints i would not be able to explain them here
[Points 3] What is the purpose of a condition variable in a monitor? How is the condition variable implemented? Explain your answer using an example.
The condition variable is defined to keep the atomicity among processes inside the monitor. The only method which can be called on condition variables are wait and signal. Once some Process has called wait on a condition, it will be suspended untill someone calls signal on this condition. Hence, in this way, we get rid of busy waiting among the processes.
Now in this example, we have kept one condition variable for each of the 5 philosophers. The strategy here is that if a philosopher wants to eat, he need to lift both of his chopsticks, otherwise he will call wait. Once some philosopher if his both neighbours are not eating. If they are eaiting, he keeps himself in suspended state, and if they are not, this philiosopher starts eating. Now once a philosopher completes eating, he goes and checks if both of his neighbours wants to eat using their state(If state is Hungary). If some of them wants to eat, this philosopher will execute a signal on that philosopher's condition variable in order to avake that.
SO if P1 wants to eat, he would check if P5 and P2 are eating.. If they are eating, P1 will suspent itself. If P1 could eat, After eating.. he will check if his neighbours P2 and P5 are in hungry state. If they are, it will call signal on their condition variables.
[Points 3] In the monitor solution to the dining-philosopher’s problem (Figure 5.18), what happens when a process i calls test(i). In other words, what is being done in test(i)?
In The method test(i), the Philosopher i checks if both of his neighbours are in eating state or not.. If they are in eating state, it means Philosopher i can't eat unless they keeps the chopsticks down. So Philosopher i doesn;t do anything. While on contrary, if both of them are not eating and Philosopher i wants to eat(State Hungary), it can easily do so. Hence it changes its own state in this case to 'Eating' and returns.. In the main method, if Philosopher i's state becoes 'Eating' it doesn't wait and starts eating.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.