• edited by
26,327 views
39 39 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 atmost 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.

5 Answers

Best answer
75 75 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
22 22 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.

 

Option B saying two processes in code section Q at any time. As signal(b) in the if condition executes when count=n, then one more signal(b) executed by processes. Due to this we can have more than 2 processes also in the code section Q. Hence option B is false.

 

Remaing options are talking about code section P. Where there is no information provided about that. Hence, you can't comment on that.
• edited by
6 6 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

0 0 votes
b is initialized to 0.

Code section Q can only be accessed when count becomes n otherwise any process which executes if condition will be blocked because of 0 as initial value of b.

To go into that condition the count has to be n.
and
count only increases after execution of code section P.

So until and unless every process executes P, the count will not be same as n.

When every process executes code section P, the condition becomes true and b becomes 1.

So one process at a time will enter CS and will execute code section Q.

Answer is A.
Answer:
Position:
Show:

Related questions

60 60 votes
4 answers 4 answers
25.1k
25.1k views
Arjun asked Feb 12, 2020
25,072 views
Consider a double hashing scheme in which the primary hash function is $h_1(k)= k \text{ mod } 23$, and the secondary hash function is $h_2(k)=1+(k \text{ mod } 19)$. Ass...
22 22 votes
3 answers 3 answers
10.7k
10.7k views
Arjun asked Feb 12, 2020
10,672 views
Consider the following five disk access requests of the form (request id, cylinder number) that are present in the disk scheduler queue at a given time.$(P, 155), (Q,85),...
33 33 votes
3 answers 3 answers
19.1k
19.1k views
Arjun asked Feb 12, 2020
19,091 views
Consider the following set of processes, assumed to have arrived at time $0$. Consider the CPU scheduling algorithms Shortest Job First (SJF) and Round Robin (RR). For RR...
129 129 votes
14 answers 14 answers
92.5k
92.5k views
Arjun asked Feb 12, 2020
92,496 views
Consider a paging system that uses $1$-level page table residing in main memory and a $\textsf{TLB}$ for address translation. Each main memory access takes $100$ ns and $...