edited by
20,373 views
69 votes
69 votes

Two processes $X$ and $Y$ need to access a critical section. Consider the following synchronization construct used by both the processes

$$\begin{array}{|l|l|}\hline \text{Process X}  &  \text{Process Y}  \\ \hline  \text{/* other code for process X*/} & \text{/* other code for process Y */} \\  \text{while (true)} & \text{while (true)} \\ 
\text{\{} & \text{\{} \\
\quad\text{varP = true;} & \quad \text{varQ = true;} \\
\quad\text{while (varQ == true)} &\quad \text{while (varP == true)}\\ 
\quad\text{\{} & \quad\text{\{}  \\ 
\quad\quad\text{/* Critical Section */} & \quad\quad\text{/* Critical Section */} \\ 
\quad\quad\text{varP = false;} &\quad\quad \text{varQ = false;} \\
\quad\text{\}} & \quad\text{\}} \\
\text{\}} & \text{\}} \\
\text{/* other code for process X */} & \text{/* other code for process Y */}\\
\\ \hline  \end{array}$$

Here varP and varQ are shared variables and both are initialized to false. Which one of the following statements is true?

  1. The proposed solution prevents deadlock but fails to guarantee mutual exclusion
  2. The proposed solution guarantees mutual exclusion but fails to prevent deadlock
  3. The proposed solution guarantees mutual exclusion and prevents deadlock
  4. The proposed solution fails to prevent deadlock and fails to guarantee mutual exclusion
edited by

6 Answers

Best answer
107 votes
107 votes

When both processes try to enter critical section simultaneously, both are allowed to do so since both shared variables varP and varQ are true. So, clearly there is NO mutual exclusion. Also, deadlock is prevented because mutual exclusion is one of the necessary condition for deadlock to happen. Hence, answer is (A).

edited by
39 votes
39 votes

the answer is A.. the main thing here to watch in question is that there is no semicolon after while loop. and so When both processes try to enter critical section simultaneously,both are allowed to do so since both shared variables varP and varQ are true. So, clearly there is NO mutual exclusion. Also,deadlock is prevented because mutual exclusion is one of the conditions for deadlock to happen. 

1 votes
1 votes

With having context switch at every instance, we can see that at one time, both X and Y are in Critical section, and after one such iteration of the while loop either of X or Y depending on the order of context switches, one of them, will move out from The Critical section.

And only one of them will be there forever.

This clearly shows that initially, there was no mutual exclusion as we got one case where both P and Q were in the critical section.

 

There is essentially no deadlock

But there is no progress and no bounded waiting.

0 votes
0 votes
Answer=D

Can anyone please explain how two processes arrive into deadlock?
Answer:

Related questions

42 votes
42 votes
5 answers
2
21 votes
21 votes
4 answers
3
35 votes
35 votes
6 answers
4
go_editor asked Feb 14, 2015
30,238 views
The maximum number of processes that can be in $\textit{Ready}$ state for a computer system with $n$ CPUs is :$n$$n^2$$2^n$Independent of $n$