edited by
6,490 views
27 votes
27 votes

The wait and signal operations of a monitor are implemented using semaphores as follows. In the following,

  • $x$ is a condition variable,
  • mutex is a semaphore initialized to $1$,
  • $x$_sem is a semaphore initialized to $0$,
  • $x$_count is the number of processes waiting on semaphore $x$_sem, initially $0$,
  • next is a semaphore initialized to $0$,
  • next_count is the number of processes waiting on semaphore next, initially $0$.

The body of each procedure that is visible outside the monitor is replaced with the following:
                            

P(mutex);
...
body of procedure
...
if (next_count > 0)
    V(next);
else
    V(mutex);


Each occurrence of $x$.wait is replaced with the following:
                        

x_count = x_count + 1;
if (next_count > 0)
    V(next);
else
    V(mutex);
------------------------------------------------------------ E1;
x_count = x_count - 1;


Each occurrence of $x$.signal is replaced with the following:
                            

if (x_count > 0)
{
    next_count = next_count + 1;
    ------------------- E2;
    P(next);
    next_count = next_count - 1;
}


For correct implementation of the monitor, statements $E1$ and $E2$ are, respectively,

  1. $P(x\_sem), V(next)$
  2. $V(next), P(x\_sem)$
  3. $P(next), V(x\_sem)$
  4. $P(x\_sem), V(x\_sem)$
edited by

2 Answers

Best answer
20 votes
20 votes
  • x_count is the number of processes waiting on semaphore x_sem, initially 0,

x_count is incremented and decremented in x.wait, which shows that in between them wait(x_sem) must happen which is P(x_sem). Correspondingly V(x_sem) must happen in x.signal. So, D choice.


What is a monitor?

selected by
9 votes
9 votes

We need value for x.wait and x.signal

We are working on 3 semaphores, mutex,  x_sem, next .

Now, check the first code

x_count = x_count + 1;

So, x_count becomes 1

but next_count still 0

So, unable to enter in the first block x.wait

if (next_count > 0)

and E1 will not execute first

So, going to execute x.signal

here x.count >0 satisfying the condition

if block permit increment next_count to be 1

 next_count = next_count + 1;

Now we want to execute x(ultimate we want to execute x)

So, V(x_sem) will be executed here

Wait(next) or P(next) will stop to execute code

Now , go to first part of code

if (next_count > 0)
    V(next);
else
    V(mutex);
------------------------------------------------------------ E1;
x_count = x_count - 1;

Now, next_count>0 is true.

So, code gets executed. V(next) and V(mutex)  gets executed here. And also x_sem already executed. So, now,P(x_sem) will be executed for Mutual Exclusion.

Answer:

Related questions

30 votes
30 votes
4 answers
3
Ishrat Jahan asked Oct 31, 2014
12,760 views
The arrival time, priority, and duration of the CPU and I/O bursts for each of three processes $P_1, P_2 $ and $P_3$ are given in the table below. Each process has a CPU ...
28 votes
28 votes
2 answers
4