Consider two processes and accessing the shared variables and protected by two binary semaphores and respectively, both initialized to 1. and denote the usual semaphore operators, where decrements the semaphore value, and increments the semaphore value. The pseudo-code of and is as follows:
In order to avoid deadlock, the correct operators at , , and are respectively.
View worked solution
Answer: D
To prevent deadlock among competing processes, we must eliminate at least one of the four Coffman conditions. The most common way to prevent circular wait is to enforce a strict global ordering on resource acquisition.
Both processes and access the shared variables and , which are protected by the binary semaphores and respectively. Since both processes modify both variables, each process must acquire both semaphores before entering its critical section.
Analyzing the Options:
- Option A: requests then , while requests then .
Deadlock scenario: executes and is preempted. executes . Now, is blocked waiting for (held by ), and is blocked waiting for (held by ). This results in a deadlock. - Option B: requests then , while requests then .
Deadlock scenario: executes and is preempted. executes . Similar to Option A, this leads to a deadlock. - Option C: requests twice, and requests twice. This does not protect both shared variables for either process and is incorrect.
- Option D: Both and request the semaphores in the exact same order: first, followed by .
Why this avoids deadlock: Whichever process successfully executes first will proceed to request . The other process will be blocked at the very first step () and cannot hold any resource while waiting. Since no circular wait can form, deadlock is completely avoided.
Thus, the correct operators at and are respectively:
Free practice · Your answers on this page are not saved.
