Question
Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) 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 P operations by the processes?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2013 answer key
Solution
Apply Dijkstra's resource hierarchy method to test which ordering of semaphore operations prevents circular wait.
To prevent deadlock among concurrent processes, the necessary condition of circular wait must be broken. Imposing a global linear order on resources guarantees deadlock freedom.
Let us analyze Option B:
- Process X:
- Process Y:
- Process Z:
Notice that:
- Semaphore is the first semaphore requested by both X and Y. Because is a binary semaphore initialized to 1, exactly one of X or Y can acquire .
- If X acquires , Y cannot proceed at all (blocked on its first operation ).
- If Y acquires , X cannot proceed at all (blocked on its first operation ).
- Suppose X acquires :
- Y is blocked at its start.
- Process Z begins by acquiring . If is available, Z acquires and proceeds to and . Neither X nor Y holds or , so Z acquires both, enters its critical section, and signals .
- Once Z signals , X acquires , then , completes, and signals .
- Finally, Y unblocks on and runs to completion.
- Suppose Y acquires :
- X is blocked at its start.
- Z acquires , while Y acquires and . Process Y completes and releases . Then Z acquires , completes, and releases . Finally X executes.
In all possible interleavings of Option B, no cycle of waiting processes can ever develop.
In contrast, Option A allows the classic circular wait: X holds waiting for , Y holds waiting for , and Z holds waiting for , causing deadlock.
Option (B)
Overlooking that Option A creates a direct circular wait cycle when each process acquires its first semaphore.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2013 Computer Science and Information Technology (CS)
- Question
- 16 of 65, printed as CS Q.16
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.