Question 171: Does the following solution solve the critical section problem?
while(true) {
while (flag); // Spin lock
flag = true;
// critical section
flag = false;
// remainder section
}
Options:
- No, there should be ‘if’ condition instead of ‘while’.
- Yes, it solves the problem.
- No, this violates the mutual exclusion requirement as both processes can be in critical region if context switch happens just after ‘while’ loop breaks but before setting flag as true.
- No, this violates the mutual exclusion requirement as both processes can be in critical region if context switch happens just before ‘while’ loop breaks and before setting flag as true.
Solution: The correct answer is the third one. Mutual exclusion is not ensured here, since both processes can enter simultaneously into the critical section in the following case: Process 1 checks the flag as false initially and before making it true, a context switch happens and Process 2 also checks the flag as false. Now, both set the flag as true and are in the critical section.