• edited by
36,767 views
100 100 votes

The $P$ and $V$ operations on counting semaphores, where s is a counting semaphore, are defined as follows:

$P(s):$

$s=s-1;$
If $s < 0$ then wait;

$V(s):$

$s=s+1;$
If $s \leq0$ then wake up process waiting on s;

Assume that $P_b$ and $V_b$ the wait and signal operations on binary semaphores are provided. Two binary semaphores $x_b$ and $y_b$ are used to implement the semaphore operations $P(s)$ and $V(s)$ as follows:

$P(s):$ 

$\quad P_b(x_b);$
$\quad s = s-1;$
$\quad \text{if } (s<0)$
$\quad \{$
$\qquad V_b(x_b);$
$\qquad  P_b(y_b); $
$\quad \}$
$\quad \text{ else } V_b(x_b); $

$ V(s):$ $\quad P_b(x_b);$
$\quad s= s+1;$
$\quad \text{if } (s\leq0) V_b(y_b) ;$
$\quad V_b(x_b);$

The initial values of $x_b$ and $y_b$ are respectively

  1. $0$ and $0$
  2. $0$ and $1$
  3. $1$ and $0$
  4. $1$ and $1$

10 Answers

Best answer
186 186 votes

Answer is (C) .

Reasoning :-

First let me explain what is counting semaphore & How it works. Counting semaphore gives count, i.e. no of processes that can be in Critical section at same time. Here value of $S$ denotes that count. So suppose $S = 3$, we need to be able to have $3$ processes in Critical section at max. Also when counting semaphore $S$ has negative value we need to have Absolute value of $S$ as no of processes waiting for critical section.

(A) & (B) are out of option, because $Xb$ must be $1$, otherwise our counting semaphore will get blocked without doing anything. Now consider options (C) & (D).

Option (D) :-

$Yb = 1, Xb = 1$

Assume that initial value of $S = 2$. (At max $2$ processes must be in Critical Section.)

We have $4$ processes, $P1, P2, P3 \& P4.$

$P1$ enters critical section , It calls $P(s) , S = S - 1 = 1.$ As $S > 1$, we do not call $Pb(Yb)$.

$P2$ enters critical section , It calls $P(s) , S = S - 1 = 0.$ As $S >0$  we do not call $Pb(Yb).$

Now $P3$ comes, it should be blocked but when it calls $P(s) , S = S - 1 = 0-1 = -1$ As $S < 0$  ,Now we do call $Pb(Yb)$. Still $P3$ enters into critical section & We do not get blocked as $Yb$'s Initial value was $1$.

This violates property of counting semaphore. $S$ is now $-1$, & No process is waiting. Also we are allowing $1$ more process than what counting semaphore permits.

If $Yb$ would have been $0, P3$ would have been blocked here & So Answer is (C).

$Pb(yb);$

• edited by
19 19 votes

Answer is (C).


So, there has been a question in the comment section, where is the Critical Section here.
Critical Section always be in the middle of P(s) and V(s).
So, basically the code block should look like below – 

P(s):   

Pb(xb);
s=s−1;
if (s<0)
{
    Vb(xb);
    Pb(yb);
}
else 
Vb(xb);

========================
Critical Section 
========================
 

V(s):   

Pb(xb);
s=s+1;
if (s≤0)
    Vb(yb);
    Vb(xb);

Now, to solve the question, we can assume whatever the value we want for ‘s’
Let’s say, I’m assuming the value of s = 3 which means maximum 3 processed can be inside the Critical Section at any point of time.

Now, Process P1 comes and it executes in the below manner – 

Step 1: P1 executes the Pb(Xb) and makes the value of Xb = 0 (Binary Semaphore, Xb value must have to be 1 initially, else no process can get into the code block)
Step 2: s = s – 1;   –  So, I assumed initially ‘s’ value as 3 and now, ‘s’ value would 2
Step 3: if(s < 0)      –  This block doesn’t execute since value ‘s’ is not less than 0
Step 4: else Vb(Xb);  – Yes, this block is executed and again Xb becomes available for a next process with it’s value as 1
Step 5: P1 gets into the Critical Section happily :) 

 

Now, Process P2 comes and it executes in the below manner – 

Step 1: P2 executes the Pb(Xb) and makes the value of Xb = 0 
Step 2: s = s – 1;   –  So, new ‘s’ value would be 1 from 2
Step 3: if(s < 0)      –  This block doesn’t execute since value ‘s’ is not less than 0
Step 4: else Vb(Xb);  – Yes, this block is executed and again Xb becomes available for a next process with it’s value as 1
Step 5: P2 also gets into the Critical Section happily and with a big smile :) 

 

Now, Process P3 comes and it executes in the below manner – 

Step 1: P3 executes the Pb(Xb) and makes the value of Xb = 0 
Step 2: s = s – 1;   –  So, new ‘s’ value would be 0 from 1
Step 3: if(s < 0)      –  This block doesn’t execute since value ‘s’ is not less than 0
Step 4: else Vb(Xb);  – Yes, this block is executed and again Xb becomes available for a next process with it’s value as 1
Step 5: P3 also gets into the Critical Section

 

Now, Process P4 comes and it executes in the below manner – (From here the actual DRAMA begins)

Step 1: P3 executes the Pb(Xb) and makes the value of Xb = 0 
Step 2: s = s – 1;   –  So, new ‘s’ value would be -1 from 0
Step 3: if(s < 0)      –  Now, this block gets execute since value ‘s’ is less than 0
Step 4: Vb(xb);   –  So, new value of Xb would be 1 again and makes itself available for the next process
             Pb(Yb);  –  Now, if we keep the Value of Yb as 1, then it would be DANGEROUS. Because, then Yb value would be 0 from 1 due to the Pb wait operation and the total IF() block would be successfully executed.
And, Process P4 will also get into the Critical Section.
That means, even though we assumed the value of S=3 initially, still total 4 process have gone inside the Critical Section, which is against the definition/property of Counting Semaphore.

So, Yb has to be 0 and that way, P4 can’t execute the IF() block successfully and keeps executing that and stays in the Wait queue, till a process doesn’t come out of the Critical Section.

Conclusion:

No way value of Binary Semaphore (or, Mutex) Xb can be 0. Then no process would be able to get into the code block and this whole design in the question would be useless.

No way value of Mutex, Yb can be 1. Then, then it would be against the property of the Counting Semaphore.

N.B.: The main funda of this question is, preparing a Counting Sempahore using 2 Binary Semaphores. It is an exclusive example of this funda.

10 10 votes
Answer is (c)

Xb must be 1 because both P(S) and V(S) operations perform Pb(Xb) first. So if Xb= 0 then all the processes performing these operations will be blocked.

Yb must be 0. otherwise two processes can be in critical section at the same time, when s=1 and Yb=1 . So Yb must not be 1.
• edited by
6 6 votes

In the given above question, we need to analyse that here counting semaphore is implemented with the help of binary semaphore.

Now let us compare both $P(s)$ code, if we see we can find that in second one they are using a binary semaphore for $s$ which implies that at a time only one process will access counting semaphore variable. 

So options a and b can be eliminated as if $x_b=0,$ then no process can access $s$.

so $x_b$ is 1 .

Now the answer can be either c or d. 

Now for $y_b$:

In the description of P(s), it is mentioned that if $s < 0,$ process must wait and so in our implementation $y_b$ must be 0 as then only $P_b(y_b)$ does wait (definition of P operation on binary semaphore). So, initial value of $y_b$ must be 0. 

So, option C.

Here $x_b$ is used to ensure that only one process access S at a time and $y_b$ ensures a wait operation if $s$ becomes negative. ($s$ being a counting semaphore it does allow multiple processes to access the critical section based on the initial value. If initial value of $s$ is 1, it becomes a binary semaphore)

 

3 3 votes

option A & option B: fails coz if $x_b = 0$ initially then it's a state of deadlock, nothing happens.

option D:
question demans the functionality that if s<0 then Wait
So, if $y_b = 1$ initially then on execution of P(s) in case when $s=0$ initially; It will successfully first decrement $x_b$ then $s$ and then signal $x_b$ then decrement $y_b$ and exit. NO waiting by P(s) happened even though s<0. This failed to implement the functionality.

option C:
suggests that keep $y_b = 0$ initially; So, if P(s) is executed given that $s=0$ initially then it will be suspended on performing $P_b(y_b)$ operation, it will continue to wait until $s$ is incremented, increment of $s$ will still be possible and also functionality of resuming a sleeping process will happen. both Functionality implemented successfully here.

answer = option C

• edited by
1 1 vote

This question was asked in gate 2008 in a different way.Anyway it will clear ur doubt I hope otherwise I will try to explain further.It is actually the implementation of a counting semaphore by 2 binary semaphores:

The P and V operations on counting semaphores, where s is a counting semaphore, are defined as follows:

P(s) : s =  s - 1; 
if (s  < 0) then wait;
V(s) : s = s + 1;
if (s <= 0) then wakeup a process waiting on s;  

Assume that Pb and Vb the wait and signal operations on binary semaphores are provided. Two binary semaphores Xb and Yb are used to implement the semaphore operations P(s) and V(s) as follows:

P(s) : Pb(Xb);    
s = s - 1;
if (s < 0) {
    Vb(Xb) ;
    Pb(Yb) ;
    }
else Vb(Xb);
V(s) : Pb(Xb) ;
s = s + 1;
if (s <= 0) Vb(Yb) ;
Vb(Xb) ;  

The initial values of Xb and Yb are respectively
(A) 0 and 0
(B) 0 and 1
(C) 1 and 0
(D) 1 and 1

Answer (C)
Both P(s) and V(s) operations are perform Pb(xb) as first step. If Xb is 0, then all processes executing these operations will be blocked. Therefore, Xb must be 1.
If Yb is 1, it may become possible that two processes can execute P(s) one after other (implying 2 processes in critical section). Consider the case when s = 1, y = 1. So Yb must be 0.

Answer:
Position:
Show:

Related questions

74 74 votes
4 answers 4 answers
35.7k
35.7k views
Kathleen asked Sep 12, 2014
35,717 views
Which of the following are NOT true in a pipelined processor?Bypassing can handle all RAW hazardsRegister renaming can eliminate all register carried WAR hazardsControl h...
475 475 votes
19 answers 19 answers
121k
121k views
Kathleen asked Sep 12, 2014
120,591 views
A processor uses $36$ bit physical address and $32$ bit virtual addresses, with a page frame size of $4$ Kbytes. Each page table entry is of size $4$ bytes. A three level...
44 44 votes
4 answers 4 answers
23.7k
23.7k views
Kathleen asked Sep 12, 2014
23,709 views
A process executes the following codefor(i=0; i<n; i++) fork();The total number of child processes created is$n$$2^n-1$$2^n$$2^{n+1} - 1$
39 39 votes
7 answers 7 answers
20.6k
20.6k views
Kathleen asked Sep 12, 2014
20,610 views
For a magnetic disk with concentric circular tracks, the seek latency is not linearly proportional to the seek distance due tonon-uniform distribution of requestsarm star...