You are on page 1of 10

Using

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

monitor <name_monitor > // Declare monitor


shared for processes
{
<shared variables>;
<condition variables>;
<exclusive methods>;
}
//process Pi:
while (1) // structure of process i
critical-section {
Noncritical-section ();
<name_monitor>. Method_i; //execute
the exclusive job i
Noncritical-section ();
}
Using Monitors structure
• The risk of execution wrong synchronization is greatly reduced.
• Very few languages support monitor structure.

"Busy and waiting" solutions do not have to


do context switching while the "sleep and
wakeup" solution will take time for this.
Monitors and the 5 philosophers have
dinner Problem

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

// exclusive methods (critical-sections)


void init();// initiating method
void test(int i); // test condition before take philosopher i having dinner
void pickup(int i); // pickup method
void putdown(int i); // putdown method
}
Monitors and the 5 philosophers have dinner
Problem
void philosopher()// initiating method (constructor)
{ // assigning the initial state to philosophers is "thinking"
for (int i = 0; i < 5; i++) state[i] = thinking;
}
void test(int i)
{ // If philosopher_i is hungry and the philosopher on the left and right are not
eating, then give philosopher_i food
if ( (state[i] == hungry) && (state[(i + 4) % 5] != eating) &&(state[(i + 1) % 5] !=
eating))
{
self[i].signal();// wake up philosopher_i, if philosopher_i is waiting
state[i] = eating; // philosopher_i is eating
}
}
Monitors and the 5 philosophers have dinner
Problem
void pickup(int i)
{
state[i] = hungry; // philosopher_i is hungry
test(i); // Check before sending food to philosopher_i
if (state[i] != eating) self[i].wait(); // waiting resource
}
void putdown(int i)
{
state[i] = thinking; // philosopher_i is thinking
test((i+4) % 5); // check philosopher on the right, if OK take this philosopher having
dinner
test((i+1) % 5);// check philosopher on the left, if OK take this philosopher having
dinner
}
Monitors and the 5 philosophers have dinner
Problem
// Structure of the Process_Pi execute the having dinner of philosopher_i
philosopher pp; //shared monitor variable
Pi:
while (1)
{
Noncritical-section ();
pp.pickup(i); // pickup is critical-section and access exclusively
eat(); // eating
pp.putdown(i);// putdown is critical-section and access exclusively
Noncritical-section ();
}
Using Message

• A process that controls using of resource and


many other processes that require resources.
while (1)
{
Send(process controler, request message); //call requesting message resource and change
to blocked
Receive(process controler, accept message); //receive accepted message using resource
critical-section (); // using shared resource exclusively
Send(process controler, end message); // call ending message using resource
Noncritical-section ();
}

In distributed systems, the message exchange mechanism


will be simpler and be used to solve the synchronization
problem.

You might also like