[Points 3] What is the purpose of a condition variable in a monitor? How is the
ID: 3847874 • 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)?
monitor DiningPhilosophers enum THINKING, HUNGRY, EATING) state C5]; condition self [5]; void pickup int i) 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 [(i 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 A monitor solution to the dining-philosopher problem.Explanation / Answer
DINING PHILOSOPHER'S PROBLEM USING MONITORS
Monitor can be used to solve the dining philosopher's problem.The philosopher can grab left and right chopsticks only if both of them are available in order to avoid hold and wait situation.
a) An array of datatype "condition" is declared which is used when philosopher is hungry and wants to pickup chopsticks.A monitor is created named "DiningPhilosophers" which has 3 procedures which are pickup(), putdown(), test().
The philosopher must follow sequence
pickup();
-----------
EATING;
---------
putdown();
-----------
THINKING;
-------------
b) Since, there are three states for the philosophers those are thinking, hungry, eating. These are implemented by
enum{THINKING, HUNGRY, EATING} states[5];
In test(i), The neighbours those are left and right of a philosopher 'i' are accessed by (i+4)%5 and (i+1)%5. According to the problem the philosopher can have chopsticks only when [(i+4)%5]!=eat and [(i+1)%5]!=eat. That means both are not eating. So, test(i) determines wheather both the neighbouring philosophers are wheather eating or not if both the chopsticks are free then the philosopher can eat.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.