Processes P1 and P2 use critical_flag in the following routine to achieve…
2007
Processes P1 and P2 use critical_flag in the following routine to achieve mutual exclusion. Assume that critical_flag is initialized to FALSE in the main program.
get_exclusive_access ( )
{
if (critical _flag == FALSE) {
critical_flag = TRUE ;
critical_region () ;
critical_flag = FALSE;
}
}Consider the following statements.
i. It is possible for both P1 and P2 to access critical_region concurrently.
ii. This may lead to a deadlock.
Which of the following holds?
- A.
(i) is false and (ii) is true
- B.
Both (i) and (ii) are false
- C.
(i) is true and (ii) is false
- D.
Both (i) and (ii) are true
Attempted by 194 students.
Show answer & explanation
Correct answer: C
Key insight: the test (if critical_flag == FALSE) and the assignment (critical_flag = TRUE) are not executed atomically. This allows a race where both processes observe the flag as FALSE before either writes TRUE.
Example interleaving that leads to concurrent access:
Process P1 evaluates the condition and sees critical_flag == FALSE, then is preempted before setting the flag.
Process P2 runs, evaluates the condition, sees FALSE, sets critical_flag = TRUE, and enters critical_region().
P1 resumes, sets critical_flag = TRUE (overwriting the flag), and enters critical_region() as well. Both processes are now inside the critical region concurrently.
Deadlock analysis: deadlock requires processes to be waiting indefinitely for resources held by each other. In this code there is no waiting or blocking on the flag; instead, there is a race leading to simultaneous entry. Therefore, no deadlock occurs.
Conclusion: the statement that concurrent access to the critical region is possible is true, and the statement that this leads to deadlock is false.