Sunday, 3 June 2018

What is Monitor in Operating System

What is Monitor in Operating System:


Monitor: 

 Monitor is a high level implementation technique to solve the critical section problem. It provides abstraction for shared data variable.

 Syntax of monitor is as following-

type  monitor_name = monitor

Section -1 :

  Declaration of shared data variables.

Section-2:

Procedure Procedure_name P1()
{

}
Procedure Procedure_name P2()
{

}
Procedure Procedure_name P3()
{

}

Section - 3: 

   Initialization of code 
{

}

The above monitor code is consist three section-

Section - I:
  
In section first we declare all the shared data items which are use in the given problem.

Section-II:
 
In this section we declare all the methods / operations involve in the given problem.

Section -III:
  In this section we initialize the variables and procedures.



 

To access monitor the process use condition stub as following-

var : x, y : condition ; to access the shared data item only two preemptive are involve [wait () & signal ( ) ].

for example:

To block a process-

x.wait( ) function are used means that all the processes are block until another process resume it.

and Second Operation-

y . signal ( ) means that particular process are resume which are suspended by any other process or operation.







Dining Philosopher Problem using Monitor:


Syntax:

 type monitor name: DP

var : state [ 0 - - - - 4 ] of (hungry, thinking , eat );
var : self [ 0 - - - - -4 ] of condition;
procedure pickup ( i: 0 - - - - 4 ] 

begin 

state [i] =hungry;
test (i) ;
if state [i] != Eat;
self [i] .wait;
end

procedure put-up [ i: 0 - - - - 4 ]
begin
state [i] = thinking;

test (i mod 5 );
test ( i + 1 mod 5 ) ;
end

Procedure test ( i : 0 - - - - 4 )
begin 
if ( state [ i + 4  mod 5] != eat and state [ i + 1 mod 5] != eat and state [i] = hungry

begin
state [i] =Eat ;
self [i] .signal ;
end

End

initialization section:

for ( i=0 ; i<= 4 ; i ++ )

state [i] =think;

Producer Consumer Problem using Monitor:

int itemconut =0;
procedure producer ( )
{
while ( true )
{
item =produceitem( );
if ( itemcount == BUFFER_SIZE )
{
sleep ( );

}
Put itemIntoBuffer (item );
itemcount =itemcount + 1;
if (itemcount = = 1)
{
wakeup ( consumer );
}
}
}


procedure consumer ()
{

while (true)
{
if (itemcount == 0)
{

sleep();
}
item=removeitemformbuffer ();
itemcount = itemcount -1 ;
if(itemcount == BUFFER_SIZE -1 )
{
wakeup (producer);
}
consumeritem (item);
}
}



No comments:

Post a comment

Popular Posts