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...