• edited by
10,756 views
31 31 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)$

2 Answers

Best answer
23 23 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 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:
Position:
Show:

Related questions

53 53 votes
6 answers 6 answers
21.8k
21.8k views
Ishrat Jahan asked Oct 31, 2014
21,836 views
Consider the solution to the bounded buffer producer/consumer problem by using general semaphores $S, F,$ and $E$. The semaphore $S$ is the mutual exclusion semaphore ini...
57 57 votes
11 answers 11 answers
93.2k
93.2k views
Ishrat Jahan asked Nov 1, 2014
93,230 views
A router uses the following routing table:\begin{array}{|l|l|l|} \hline \textbf {Destination} & \textbf { Mask} & \textbf{Interface} \\\hline \text {144.16.0.0} & \text...
46 46 votes
7 answers 7 answers
21.9k
21.9k views
Ishrat Jahan asked Oct 31, 2014
21,888 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 ...
47 47 votes
3 answers 3 answers
20.4k
20.4k views
Ishrat Jahan asked Oct 31, 2014
20,352 views
The process state transition diagram of an operating system is as given below.Which of the following must be FALSE about the above operating system?It is a multiprogramme...