Question

In: Computer Science

Considering the following algorithm: bool thread1 = false; bool thread2 = false; thread1 { While (true)...

Considering the following algorithm:

bool thread1 = false;

bool thread2 = false;

thread1 {

While (true) {   

    while(thread2 == true);      

    thread1 = true;  

               /* start of critical section */     

    …   ...

    /* end of critical section */  

    thread1 = false  

               …  

}   

}  

thread2 {

While (true) {   

    while(thread1 == true);      

    thread2 = true;  

               /* start of critical section */     

    …     

    /* end of critical section */  

    thread2 = false  

               …  

}   

}  

Does this solution have mutual exclusion?

Solutions

Expert Solution

Mutual exclusion allows only one process to enter the critical section at a time.

Initially the values of thread1 and thread2 both are false.

Let process 1 correspond to thread1 and process 2 correspond to thread2.

In the given segment, suppose Process1 executes till the line :

while(thread2==true);

and this will evaluate the false as thread2 = false, and then context switch takes place to Process 2.

Now the values of both thread1 and thread2 are still false.

Now let Process 2 executes till the line :

while(thread1==true);

and this will evaluate to false as thread1 = false. Then, let thread2 execute the remaining line and enter the critical section.

Now the value of thread2 = true and thread1 = false.

So, when Process 2 is in critical section, let Process 1 perform the remaining lines and enter the critical section.

Process 1 will update the value of thread1 from false to true and proceed towards critical section.

In this way, both Process 1 and Process 2 are present inside the critical section at the same time.

This violates the definition of mutual exclusion.

Hence, the solution does not have mutual exclusion.


Related Solutions

(True or False) The following is an infinite loop. int main() { bool flag = false;...
(True or False) The following is an infinite loop. int main() { bool flag = false; int num = 0; while(!flag); { cin >> num; if(num == -1) { flag = true; } } return 0; }
True or False) The following code will output "I was true". bool isGreater(string s1, string s2)...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }
True or False: Futures are customizable contracts while Forwards are not. True False
True or False: Futures are customizable contracts while Forwards are not. True False
Choose true or false for each statement regarding the resistance of a wire. true false  While maintaining...
Choose true or false for each statement regarding the resistance of a wire. true false  While maintaining a constant voltage V, the current I increases when the length L of a wire increases. true false  The resistance is proportional to the cross-sectional area A of the wire. true false  While maintaining a constant voltage V, the current I decreases when the resistivity ρ of a wire decreases. Choose true or false for each statement regarding resistors in a circuit. true false  If you connect...
True or False if false state why: While she initially committed to becoming a nun and...
True or False if false state why: While she initially committed to becoming a nun and serving the poor, Austrian Catholic immigrant descendant Philadelphian Katherine Drexel soon found her vow of poverty to be tiresome, and she subsequently returned to her life of wealth and privilege in Philadelphia and built a spectacular mansion with all of her millions. ____
True/False: Indicate whether the following statement is true or false. If it is false, indicate the...
True/False: Indicate whether the following statement is true or false. If it is false, indicate the reason. If it is true, indicate what makes it true. Just stating true or false will not earn any points. "It is not possible to have concave preferences."
True or False Determine if the following statements are true or false. _____ 1. A liquid...
True or False Determine if the following statements are true or false. _____ 1. A liquid takes the volume of its container. _____ 2. Particles of amorphous solids have no definite pattern. _____ 3. A beef steak is an example of a crystalline solid. _____ 4. Viscosity causes water to curve upward at the top rim of a glass. _____ 5. There is more gas than any other state of matter in the universe. _____ 6. All states of matter...
1.While the biceps is contracting, there is no activity in the triceps. True or false 2.As...
1.While the biceps is contracting, there is no activity in the triceps. True or false 2.As contractile strength increases, ____________ muscle motor units become involved in the contraction. More or ess 3.A maximum strength skeletal muscle contraction can be sustained indefinitely. True or false
True/False (circle one) AdaBoost is a boosting algorithm that has been described as one of the...
True/False (circle one) AdaBoost is a boosting algorithm that has been described as one of the best “off-                                        the-shelves” classifier. AdaBoost always outperforms the simpler bagging algorithm.
Answer the following questions True or False. Write “True” or “False” in the blank. 1._________ The...
Answer the following questions True or False. Write “True” or “False” in the blank. 1._________ The rate constant changes if the concentrations of the reactant change. 2._________ If the concentration of a reactant changes, the rate of the reaction always changes. 3._________ The coefficients of the overall reaction tell us the order for each reactant. 4._________ The half-life for a first order reaction does not depend on the initial concentration. 5._________ If the rate of a reaction over a long...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT