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.