GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Detect deadlock from waiting for an event while holding its mutex

2 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Both have an initially available mutex and an initially unavailable event semaphore. One participant acquires the mutex and then waits for the event without releasing it, while the participant that can signal the event first needs that same mutex.

2 marks · MSQ
Report issue

A semaphore ordering that can deadlock

Deadlocks

Question 46, GATE 2024: Threads share s1=1,s2=0,x=0. T1: wait(s1); x++; print(x); wait(s2); signal(s1). T2: wait(s1); x++; print(x); signal(s2); signal(s1). Possible outcomes? A T1 prints 1 then T2 prints 2; B T2 prints 1 then T1 prints 2; C T1 prints 1 then deadlock without T2 printing; D T2 prints 1 then deadlock without T1 printing.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C

Approach: Branch on which thread acquires s1 first, since this excludes the other thread from its increment.

1. Derivation. The first successful wait(s1) excludes the other thread. If T1 acquires it first, T1 increments and prints 1, then blocks on initially zero s2 while still holding s1; T2 cannot reach its signal(s2), so this deadlocks, giving C. If T2 acquires first, it prints 1 and signals s2 and s1. T1 can then acquire s1, print2, consume s2, and finish, giving B. Thus B, C.

2. Explanation and checks. In the T1-first branch the wait-for chain is T1 waiting for s2, which only T2 signals; T2 waits for s1, which T1 will not signal until its s2 wait finishes. In the T2-first branch signal(s2) either stores a permit or satisfies a waiting thread, so T1’s later wait(s2) cannot deadlock. The mutual exclusion around x prevents a lost increment, but it does not prevent the circular wait caused by the semaphore ordering.

Original paper, page 32 · Answer key

2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 31, GATE 2014: Producer-consumer pseudocode starts with semaphores n=0, s=1. Producer repeats produce(); wait(s); addToBuffer(); signal(s); signal(n). Consumer repeats wait(s); wait(n); removeFromBuffer(); signal(s); consume(). Which is true? A Producer can add but consumer can never consume. B Consumer removes at most one item. C Deadlock occurs if consumer acquires s when the buffer is empty. D n must start at 1 rather than 0 for deadlock-free operation.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Set up the reasoning. Interpret s as the mutex and n as the number of available items. Trace the particular interleaving where the consumer runs first and locks the empty buffer.

2. Derive the result. If the consumer acquires s while n=0, it blocks on n while retaining s. The producer needs s before adding an item and signaling n, so neither can proceed. That is the deadlock in C. Correct ordering is to wait for n before acquiring the buffer mutex; inventing an initial item by setting n=1 is not a valid repair.

3. Check the interpretation. The consumer holds the very lock the producer requires to make n positive. The proper repair is to wait on n before locking s, not to pretend that an item exists by setting n=1.

Original paper, page 37 · Answer key