retagged by
13,700 views
19 votes
19 votes

Each of a set of $n$ processes executes the following code using two semaphores $a$ and $b$ initialized to $1$ and $0$, respectively. Assume that $\text{count}$ is a shared variable initialized to $0$ and not used in CODE SECTION P.

$\begin{array}{|c|} \hline \textbf{CODE SECTION P} \\ \hline \end{array}$

wait(a); count=count+1;
if (count==n) signal (b);
signal (a): wait (b) ; signal (b);

$\begin{array}{|c|} \hline \textbf{CODE SECTION Q} \\ \hline \end{array}$

What does the code achieve?

  1. It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.
  2. It ensures that two processes are in CODE SECTION Q at any time.
  3. It ensures that all processes execute CODE SECTION P mutually exclusively.
  4. It ensures that at most $n-1$ processes are in CODE SECTION P at any time.
retagged by

3 Answers

Best answer
36 votes
36 votes

Answer: A. It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.

Explanation
In short, semaphore 'a' controls mutually exclusive execution of statement count+=1 and semaphore 'b' controls entry to CODE SECTION Q when all the process have executed CODE SECTION P. As checked by given condition if(count==n) signal(b); the semaphore 'b' is initialized to 0 and only increments when this condition is TRUE.
 (Side fact, processes do not enter the CODE SECTION Q in mutual exclusion, the moment all have executed CODE SECTION P, process will enter CODE SECTION Q in any order.)

Detailed explanation:-
Consider this situation as the processes need to execute three stages- Section P, then the given code and finally Section Q.

It is evident that semaphores do not control Section P hence, There is no restriction in execution of P.

Now, we are given 2 semaphores 'a' and 'b' initialized to '1' and '0' respectively.

Take an example of 3 processes (hence n=3, count=0(initially) ) and lets say first of them has finished executing Section P and enters the given code. It does following changes:-
1. will execute wait(a) hence making semaphore a=0
2. increment the count from 0 to 1 (first time)
3. If(count==n) evaluates FALSE and hence signal(b) is not executed. So semaphore b remains 0
4. signal(a) hence making semaphore a=1
5. wait(b) But since semaphore b is already 0, The process will be in blocked/waiting state.
First out of the three processes is unable to enter the CODE SECTION Q !

Now say second process completes CODE SECTION P and starts executing the given code. It can be concluded that it will follow the same sequence (5 steps) as mentioned above and status of variables will be:- count = 2 (still count<n), semaphore a=1, semaphore b=0 (no change)

Finally the last process finishes execution of CODE SECTION P.
It will follow same steps 1 and 2 making semaphore a=0 and count = 3
3. if(count==n) evalueates TRUE! and hence signal(b) is executed marking semaphore b = 1 FOR THE FIRST TIME.
4 and 5 will be executed the same way.

Now the moment this last process signaled b, the previously blocked process will be able to execute wait(b) and the very next moment execute signal(b) to allow other blocked/waiting process to proceed.
This way all the processes enter CODE SECTION Q after executing CODE SECTION P.

edited by
10 votes
10 votes
a is initialized to 1 and b is initialized to 0.

when a process tries to perform down on b, it will be block.

 

if you observe the code, when count equals to n, then one process from blocked queue, enter into CODE SECTION Q.

so, for any process, to enter into CODE SECTION Q, count must be equals to n.

When Count equals to n ?

when all processes, completes CODE SECTION P, after that, the subsequent code executed by them, after that, count equals to n.

 

Option A is correct
5 votes
5 votes

Answer : A

Option A:  Assume few processes try to enter in Code Q and get blocked since b=0 and we have to do wait(b) ,When count = n, then one process from blocked queue, enter into Code Q.

Option B: no context of atmost 2 present in code. So, false

Option C: No in code P n-number of process can run simultaneously i.e. when signal(a) done more process can enter in Code P while other process preempt before signal(b) i.e. shared variable count shared by many processes at same time.  So, false.

Option D: Code P can have atmost n process at any time. So, false

Answer:

Related questions

51 votes
51 votes
10 answers
3