edited by
820 views
2 votes
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

edited by

2 Answers

Best answer
5 votes
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 votes
1 votes

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

Related questions

0 votes
0 votes
1 answer
1
N3314nch41 asked Sep 10, 2023
359 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 votes
0 votes
0 answers
2
Nirmal Gaur asked Dec 19, 2018
552 views
Does Progress implies freedom from Deadlock?
0 votes
0 votes
1 answer
4
rishi71662data4 asked Nov 16, 2017
611 views
In a given synchronization construct, if there is no deadlock and no strict alternation between two process then is it always true that a process will starve ?