Synchronization & Concurrency



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.