edited by
23,109 views
68 votes
68 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$
edited by

8 Answers

1 votes
1 votes

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.

1 votes
1 votes
Since atleast one process should enter in the critical section the initial value should of xb should be 1 where as the initial value of yb should be zero because in v(s) there is a if condition <= 0 and in p(s) there is a point p(yb) so if the initial value of yb is 1 then in v(s) it will be 0 then it'll satisfy condition and second process will enter into c.s so yb should be 0 so that it is value should be - so that second process cantc get enter in c.s
0 votes
0 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.

Answer:

Related questions

29 votes
29 votes
3 answers
2
Kathleen asked Sep 12, 2014
17,098 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$
23 votes
23 votes
4 answers
3
32 votes
32 votes
2 answers
4
Kathleen asked Sep 11, 2014
15,158 views
The data blocks of a very large file in the Unix file system are allocated usingcontinuous allocationlinked allocationindexed allocationan extension of indexed allocation...