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?

Choose one option
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: P(b)→P(a)→P(c)P(b) \to P(a) \to P(c)
  • Process Y: P(b)→P(c)→P(d)P(b) \to P(c) \to P(d)
  • Process Z: P(a)→P(c)→P(d)P(a) \to P(c) \to P(d)

Notice that:

  1. Semaphore bb is the first semaphore requested by both X and Y. Because bb is a binary semaphore initialized to 1, exactly one of X or Y can acquire bb.
    • If X acquires bb, Y cannot proceed at all (blocked on its first operation P(b)P(b)).
    • If Y acquires bb, X cannot proceed at all (blocked on its first operation P(b)P(b)).
  2. Suppose X acquires bb:
    • Y is blocked at its start.
    • Process Z begins by acquiring aa. If aa is available, Z acquires aa and proceeds to P(c)P(c) and P(d)P(d). Neither X nor Y holds cc or dd, so Z acquires both, enters its critical section, and signals a,c,da, c, d.
    • Once Z signals aa, X acquires aa, then cc, completes, and signals b,a,cb, a, c.
    • Finally, Y unblocks on bb and runs to completion.
  3. Suppose Y acquires bb:
    • X is blocked at its start.
    • Z acquires aa, while Y acquires cc and dd. Process Y completes and releases b,c,db, c, d. Then Z acquires c,dc, d, completes, and releases a,c,da, c, d. 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 aa waiting for bb, Y holds bb waiting for cc, and Z holds cc waiting for aa, causing deadlock.

Final answer

Option (B)

Common trap

Overlooking that Option A creates a direct circular wait cycle X→Y→Z→XX \to Y \to Z \to X 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.