The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+25 votes
4.6k views

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 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):$  $P_b(x_b); \\ \text{    } s = s-1; \\ \text{     if } (s<0) \:\{ \\ \text{      } V_b(x_b); \\\text{      } P_b(y_b); \\ \} \\ \text{    else } V_b(x_b); $
$ V(s):$ $P_b(x_b); \\\text{    } s= s+1; \\ \text{    if } (s<=0) V_b(y_b) ; \\\text{    } 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
asked in Operating System by Veteran (59.4k points) | 4.6k views
+1
Notice -> In entire above code operation that require synchronization is increment and decrement only.

means code could be optimized as follow -->

$P(s):  P_b(x_b);  s=s−1; V_b(x_b); $

              $if (s<0)  P_b(y_b); $

$V(s): P_b(x_b); s=s+1; V_b(x_b);  $

             $if (s<=0)V_b(y_b); $

Please correct me if i am wrong.
0
I have a doubt here.Even in option c) when there is busy waiting the lock on xb will be held up.Isn't deadlock a possibility here?even if some wants to release the lock it can't isn't it?

4 Answers

+54 votes
Best answer

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);

 

answered by Boss (42.4k points)
selected by
0
@akash  @priti sharma what is change here  value of Xb or s ?
0
Hi Akash. What about the value of Xb when P1 enters the critical section .? and Similarly value of Xb when process P2 is about to enter the CS and when P2 has entered the CS?
+1

@Ayush

Xb and Yb are binary semaphore, so value of Xb and Yb either 0 or 1 for sure. and it will not change during execution of program. Means before entering the CS, the value of Xb and Yb  will be same as when Xb and Yb leaving CS

+2
We know
$P(s)$ :$s=s-1;$
             If $s < 0$ then wait;

to implement wait operation in code
Vb(Xb) is not take care of it.
because V is a UP operation and allow any value inside it.
This thing will take care by Pb(Yb). Where as Yb value is 0, will cause to wait for other operation
0
I have a doubt here.Even in option c) when there is busy waiting the lock on xb will be held up.Isn't deadlock a possibility here?even if some wants to release the lock it can't isn't it?
+5 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.
answered by Junior (749 points)
edited by
+2
"Yb must be 0. otherwise two processes can be in critical section at the same time"

How is this possible?
+2
Consider the case when S=1 and Yb=1,

First process will arrive and successfully execute P(S) and it will enter into critical section.

Second process will arrive, Since Yb = 1 it will also execute P(S) successfully and enter into critical section.

But if we take Yb=0 then first process will enter into CS but second process will be blocked ( when it will execute Pb(Yb) ) .

So at a time only one process will be into CS.
+2
But we don't know the order in which the processes call P(s) and V(s). Also, we are not told to solve critical section problem here. Answer is C only, but the reason should be with respect to the given definition of counting semaphore- how the binary semaphore can implement them.
+1
Sir, the purpose of semaphore is to ensure mutual exclusion only, no? It should be implemented in such way that it ensures mutual exclusion.

So what could be the reason according to the definition of counting semaphore ?
+2
sir Yn ensure that at any point of time when s<0 DOWN operation (P(s)) shouldn't be successful and UP operation  (V(s)) always successful irrespective of s value.
+2 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)

 

answered by Active (1.2k points)
+1
mostly correct, but binary semaphore value going negative? S < 0, then processes must wait as per question. Now, for waiting yb must be 0.
0
Oh yes . binary value can take a value betwee 0 and 1 only . i got it . thanks :)
0
:D
+2 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

answered by Boss (30.6k points)
edited by


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

34,780 questions
41,758 answers
118,934 comments
41,400 users