Professional Documents
Culture Documents
Using Monitors Structure
Using Monitors Structure
UsingMonitors
Monitorsstructure
structure
• Monitor is a special structure (class)
Nhóm 4
Trần Công Hiền
Lê Nguyễn Minh Khôi
Nguyễn Hoàng Hùng
– variables (shared for processes)
• Variables in the monitor can only be accessed by methods in the monitor
– At one point, there is only one process that is operated inside a monitor.
– Condition Variable c
• used to synchronize the use of variables in the monitor.
• Wait(c) and Signal(c):
Using Monitors structure
Wait(c)
{ status(P)= blocked; // Move P to the waiting state
enter(P,f(c)); // Put P on the queue f(c) of the condition variable c
} Wait(c): switch the state of call progress to
waiting (blocked) and set this process to the
Signal(c) queue of the condition variable c.
{
if (f(c) != NULL)
{
exit(Q,f(c)); // Get the Q process waiting on c
statusQ) = ready; // Move Q to ready state
enter(Q,ready-list); // Put Q on the ready-list
} Signal(c): if there is a waiting process in the queue of c, re-
} activate that process and the calling process will leave the
monitor. If no process is waiting in the queue of c, the
Signal(c) command is ignored.
Using Monitors structure
3
4
Monitors and the 5 philosophers have dinner
Problem
monitor philosopher
{ enum {thinking, hungry, eating} state[5];// shared variables for philosophers
condition self[5]; // condition variables for synchronise the having dinner