GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 20(b)

Exhibit a lost wakeup deadlock

Operating Systems · Synchronization & Concurrency · Full GATE CSE 1999 paper

GATE 1999, Q20(b) 3 marks · Descriptive
Report issue

Synchronization & Concurrency

Question 20(b), GATE 1999: A one-slot producer-consumer buffer uses count=0 initially. Tests and assignments of count are individually atomic. Producer repeats: produce item; if count=1 then sleep; put item in buffer; count:=1; wakeup(Consumer). Consumer repeats: if count=0 then sleep; remove item; count:=0; wakeup(Producer); consume item. Show an execution where both processes sleep.
Question 20(b), GATE 1999: A one-slot producer-consumer buffer uses count=0 initially. Tests and assignments of count are individually atomic. Producer repeats: produce item; if count=1 then sleep; put item in buffer; count:=1; wakeup(Consumer). Consumer repeats: if count=0 then sleep; remove item; count:=0; wakeup(Producer); consume item. Show an execution where both processes sleep.
Question 20(b), GATE 1999: A one-slot producer-consumer buffer uses count=0 initially. Tests and assignments of count are individually atomic. Producer repeats: produce item; if count=1 then sleep; put item in buffer; count:=1; wakeup(Consumer). Consumer repeats: if count=0 then sleep; remove item; count:=0; wakeup(Producer); consume item. Show an execution where both processes sleep.
Tap to enlarge
Answer & solution

Answer: A wakeup between the consumer’s test and sleep is lost; producer later sleeps on the full buffer. (no official key was published)

Assume ordinary sleep/wakeup semantics: waking a process not currently asleep does not save a future wakeup token. The following interleaving is legal even though each test and assignment is atomic.
1. Consumer tests count=0 and gets true, but is preempted just before executing sleep.
2. Producer produces an item, tests count=1 (false), places the item, then atomically sets count=1.
3. Producer calls wakeup(Consumer). Consumer is not sleeping yet, so the notification is lost.
4. Consumer resumes at the pending sleep instruction and goes to sleep, without rechecking count. The buffer is full and count remains 1.
5. Producer begins its next iteration and produces another item. Its count=1 test is true, so it sleeps waiting for space.
Now both processes are asleep. Only the sleeping consumer could remove the buffered item and wake the producer; only the sleeping producer could issue another consumer wakeup. Individual atomicity of test and assignment does not make the combined test-and-sleep sequence atomic. A semaphore that retains notifications, or correctly synchronized condition-variable wait with a predicate loop, prevents this lost-wakeup window.

Original paper, page 9