monitor DiningPhilosophers enum THINKING, HUNGRY, EATING) state C51 condition se
ID: 3848002 • Letter: M
Question
monitor DiningPhilosophers enum THINKING, HUNGRY, EATING) state C51 condition self [5] void pickup (int i) state [il HUNGRY test (i); if (state [i] EATING) self Lil wait(); void putdown (int i) f state [i] THINKING test (i 4) 5) test (i 1) 5); void test (int i) if ((state [(i 4) 5] EATING) && (state [i] HUNGRY) && (state Cli 1) %5] EATING)) state [i] EATING self [i] signal initialization code for (int i 0; i 5; i++) state Cil THINKING Figure 5.18 A monitor solution to the dining-philosopher problem.Explanation / Answer
a)A condition variable is a container of threads that are waiting for a certain condition.
Monitors provide a mechanism for threads to temporarily give up exclusive access in order to wait for some condition to be met, before regaining exclusive access and resuming their task.
a thread in a monitor may have to block itself because of its request may not complete immediately. This waiting for an event (e.g., I/O completion) to occur is realized by condition variables.
There are only two operations that can be applied to a condition variable: wait and signal. When a thread executes a wait call (in a monitor, of course) on a condition variable, it is immediately suspended and put into the waiting queue of that condition variable. Thus, this thread is suspended and is waiting for the event that is represented by the condition variable to occur. Because the calling thread is the only thread that is running in the monitor, it "owns" the monitor lock. When it is put into the waiting queue of a condition variable, the system will automatically take the monitor lock back. As a result, the monitor becomes empty and another thread can enter.
Eventually, a thread will cause the event to occur. To indicate a particular event occurs, a thread calls the signal method on the corresponding condition variable. At this point, we have two cases to consider.First, if there are threads waiting on the signaled condition variable, the monitor will allow one of the waiting threads to resume its execution and give this thread the monitor lock back. Second, if there is no waiting thread on the signaled condition variable, this signal is lost as if it never occurs.
Therefore, wait and signal for a condition variable is very similar to the notification technique of a semaphore: One thread waits on an event and resumes its execution when another thread causes the event to occur. However, there are major differences as will be discussed on a later page.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.