Monitor using semaphore

131 views
Skip to first unread message

Rahul Shah

unread,
Mar 31, 2012, 7:56:13 PM3/31/12
to asu-cse-430
We have implemented a monitor using semaphore in which the signalling
process blocks and the process which the signalling process wak up
executes. The signalling process resumes when the woken up process
either executes or exits the monitor.

I have written the routine in which signalling process continues and
the woken up process will resume after the signalling process either
blocks or exits the monitor. Please let me know your comments

function()
{
P(mutex) // entry criteria
int wakeup_x_count=0; //per condition. The signaler uses this
count to determine if it has to let a process continue after it exits
or gets block


//Wake up
x_count++;
if(wakeup_x_count>0) //check if you had signaled any process
which was waiting on x.. if yes wake that up without releasing the
mutex ..
{
wakeup_x_count--;
V(x_sem);
}
else V(mutex) ; //if no then simple release the mutex

P(x_sem);
x_count--;

//Signal
if(x_count>0) wakeup_x_count++; //if there are processes waiting
on x wake them up otherwise nothing


//Exit section

if(wakeup_x_count>0) V(x_sem);
else V(mutex);



//Thank you...

Partha Dasgupta

unread,
Apr 1, 2012, 9:15:08 PM4/1/12
to asu-c...@googlegroups.com
There are plenty of bugs in this one.

[The correct solution can easily be adapted from the code given in class, and yes you need a mutex and a next semaphore for each monitor in addition to the wait semaphore per condition.]

 P(mutex)   // entry criteria
   int wakeup_x_count=0;     //per condition. The signaler uses this

Do not initialize the count as above. It may already have a value as something may have already blocked.

I got really confused with a comment wakeup.... you mean this is the wait part not wakeup part.

//Wake up   [wait function]
   x_count++;
   if(wakeup_x_count>0)   //check if you had signaled any process
which was waiting on x.. if yes wake that up without releasing the
mutex ..
    {
       wakeup_x_count--;
       V(x_sem);
     }

   else V(mutex)  ; //if no then simple release the mutex

    P(x_sem);
    x_count--;

This looks fine.

//Signal
   if(x_count>0) wakeup_x_count++;  //if there are processes waiting
on x wake them up otherwise nothing


//Exit section

if(wakeup_x_count>0) V(x_sem);
else V(mutex);

--- at this point using X-Sem to block is wrong, as this process is not blocked on a condition and may not be woken up when the monitor is free.

You are missing the monitor exit part.


On Sat, Mar 31, 2012 at 4:56 PM, Rahul Shah <rrs...@asu.edu> wrote:
We have implemented a monitor using semaphore in which the signalling
process blocks and the process which the signalling process wak up
executes. The signalling process resumes when the woken up process
either executes or exits the monitor.

I have written the routine in which signalling process continues and
the woken up process will resume after the signalling process either
blocks or exits the monitor. Please let me know your comments

function()
{
 

 
count to determine if it has to let a process continue after it exits

or gets block


//Wake up
   x_count++;
   if(wakeup_x_count>0)   //check if you had signaled any process
which was waiting on x.. if yes wake that up without releasing the
mutex ..
    {
       wakeup_x_count--;
       V(x_sem);
     }
   else V(mutex)  ; //if no then simple release the mutex

    P(x_sem);
    x_count--;

//Signal
   if(x_count>0) wakeup_x_count++;  //if there are processes waiting
on x wake them up otherwise nothing


//Exit section

if(wakeup_x_count>0) V(x_sem);
else V(mutex);



//Thank you...

--
You received this message because you are subscribed to the Google Groups "asu-cse-430" group.
To post to this group, send email to asu-c...@googlegroups.com.
To unsubscribe from this group, send email to asu-cse-430...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asu-cse-430?hl=en.




--
Partha Dasgupta,
School of Computing Informatics, Arizona State University
EMail: par...@asu.edu
http://cactus.eas.asu.edu/partha
Reply all
Reply to author
Forward
0 new messages