Operating System Concepts (Process Synchronization) 2. Consider the following im
ID: 3678025 • Letter: O
Question
Operating System Concepts (Process Synchronization)
2. Consider the following implementation of a "fair" counting contest (i and mutex are the shared variables):
Process A Process B
while (i < 10) while (i > -10)
{ {
wait(mutex); wait(mutex);
i = i + 1; i = i - 1;
signal(mutex); signal(mutex);
} }
printf("A wins"); printf("B wins");
Note that since reading variables is atomic, the tests of i are "safe" even though they are outside the critical sections. Suppose that both processes are started with i initialized to 0 and mutex to 1. You may make no assumptions about the scheduling algorithm used.
a) Is it obvious which process will win? Explain why or why not.
b) What happens if process B decides to cheat by omitting the signal(mutex) call? Does this guarantee that B will win? Explain.
c) What happens if B omits both the wait(mutex) and signal(mutex) calls? Does this guarantee that B will win? Why or why not?
d) What is the best way for B to cheat (by adding, removing, or moving wait() and signal() calls in its own code only)? Can it guarantee a win? Explain.
Explanation / Answer
Answer for Question 1:
Both the Cases entry conditions are true so..
If i am not make any assumptions about scheduling algorithm.
It is based on the first process arrives and lock the resources first.
until process completes it's resources it will release the resource
Based on above information Process A wins
Answer for Question 2:
No guarantee that B will wins..why because..
if more than one threads try to execute Wait (or Signal),
only one of them will succeed. We should not make any assumption
about which thread will succeed.
Answer for Question 3:
May or May not because if I is hold by Process A ..but incase of
B omits the both wait and signals..
Answer for Question 4:
The best way is moving wait and signals calls it's own code copy then may be chance of wins
the Process B.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.