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 true and (ii) is false B. Both (i) and (ii) are false C. (i) is false and (ii) is true D. Both (i) and (ii) are true
GATE 2007 · Operating System · Process Synchronization · medium
Answer: Statement (i) is true (both can access critical_region concurrently due to race condition) and statement (ii) is false (no deadlock occurs). Answer: A. (i) is true and (ii) is false
- Check statement (i): Can both P1 and P2 access critical_region concurrently?: Execution sequence that leads to concurrent access: 1. P1 executes: if (critical_flag == FALSE) → TRUE (flag is FALSE), condition holds 2. Context switch to P2 before P1 executes critical_flag = TRUE 3. P2 executes: if (critical_flag == FALSE) → TRUE (flag still FALSE, P1 hasn't set it yet) 4. P2 executes: critical_flag = TRUE 5. P2 enters and executes critical_region() 6. Context switch back to P1 7. P1 executes: critical_flag = TRUE (already TRUE, no re-check) 8. P1 enters and executes critical_region() Both P1 and P2 are in critical_region at the same time. Statement (i) is TRUE.
- Check statement (ii): Does this lead to deadlock?: In this code, if critical_flag == TRUE, the process simply does NOT enter the if-block — it returns from get_exclusive_access() without waiting. No process is ever blocked spinning or sleeping waiting for another process to release the flag. There is no circular wait. Therefore, no deadlock can occur. Statement (ii) is FALSE.
- Match to answer options: Statement (i) is TRUE. Statement (ii) is FALSE. This matches option A: '(i) is true and (ii) is false'.