Three concurrent processes , , and execute three different code segments that access and update certain shared variables. Process executes the operation (i.e., ) on semaphores , and ; process executes the operation on semaphores , and ; process executes the operation on semaphores , , and before entering the respective code segments. After completing the execution of its code segment, each process invokes the operation (i.e., ) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlock-free order of invoking the operations by the processes?
View worked solution
Answer: B
To ensure a system is deadlock-free, we must prevent a circular wait condition. Let us analyze the options by checking if a circular wait can be formed when processes execute concurrently.
Consider Option B:
Let's trace the execution of the first operations:
- Both and compete for semaphore as their first operation. Since is a binary semaphore initialized to , only one of them can acquire it.
- Case 1: acquires first. is blocked on . Now, and compete for semaphore .
- If acquires , holds both and . It can then easily acquire , complete its execution, and release all semaphores, allowing and to proceed.
- If acquires , blocks on . holds and can successfully acquire and (since they are free), complete its execution, and release them. This unblocks , which then completes, followed by .
- Case 2: acquires first. is blocked on . Now, (holding ) and (holding ) compete for .
- If acquires , holds and . It then acquires , completes, and releases them.
- If acquires , holds and . It then acquires , completes, and releases them.
In all possible execution sequences of Option B, no circular wait is formed, making it completely deadlock-free.
For other options, we can easily find a deadlocked state. For example, in Option A, if acquires , acquires , and acquires , a circular wait occurs (), resulting in a deadlock.
Free practice · Your answers on this page are not saved.
