• edited by
1,292 views
2 2 votes

If there are n processes executing concurrently using binary semaphore S , (n-1) processes have the code
 

do{
    wait(S);
    <c.s>
    signal(s);
    <r.s>
}while(1);



the code for the n-th process i.e Pn is given by
 

do{
    signal(S);
    <c.s>
    signal(S);
    <r.s>
}while(1);



What is the max no. of processes that can be there in the critical section simultaneously?

A. 2
B . 3
C. n-1
D. n

2 Answers

Best answer
5 5 votes

For the $n-1$ processes that use the first code, they enter their critical section only after acquiring the semaphore. But the last process $P_n$ is entering its critical section but instead of waiting for the semaphore, it signals it.

Let's suppose $P_1$ enters the critical section first. So, it sets the semaphore $S$ to 0, which was initialized to 1. (remember it's a binary semaphore).

Now, while $P_1$ is executing in its C.S., let's see what happens if $P_n$ also goes to its C.S. Before entering, it will set the semaphore $S$ to 1 by signal(S). And then goes to its C.S.

Now another process, say $P_2$ tries to enter its C.S. and it gets it because $S$ is 1. It again sets $S$ to 0 and goes to C.S.

Currently, we have 3 processes in C.S.($P_1, P_2, P_n$).

Now, if $P_n$ leaves its C.S., it will again signal(S), and sets $S$ to 1. Another process, say $P_3$ can now enter the C.S.

Then again $P_n$ goes to its C.S., which allows another process to go to C.S. 

Continuing this way, in the worst case, all the n processes can be in their critical section simultaneously.

• selected by
1 1 vote

Let us take an example : 

Now, at this moment all the 5 processes are in CS. 

Option B is right.

Sorry for clarity.

• reshown by
Position:
Show:

Related questions

0 0 votes
1 1 answer
590
590 views
0 0 votes
1 1 answer
856
856 views
N3314nch41 asked Sep 10, 2023
856 views
How to approach synchronization (specifically semaphore) question, there size are really intimidating and i’m unable to decode the code written? What to do??
0 0 votes
0 0 answers
1.1k
1.1k views
Nirmal Gaur asked Dec 19, 2018
1,118 views
Does Progress implies freedom from Deadlock?
2 2 votes
2 2 answers
1.7k
1.7k views
Kalpataru Bose asked Nov 27, 2017
1,686 views
If there are n processes executing concurrently using binary semaphore S , (n-1) processes have the codedo{ wait(S);<c.s>signal(s);<r.s>...