Microsoft Interview Question

Implement a solution for the bounded buffer problem where you have a queue of work items and several producer/consumer threads.

Interview Answers

Anonymous

Oct 20, 2009

Use a counting semaphore, incrementing each time a producer puts a new item in the queue and decrementing each time a consumer removes an item from the queue.

1

Anonymous

Aug 9, 2011

use three binarySemaphore/SemaphoreMutex(both are same) empty full access producer() { wait(empty) wait(access) // Fill the q signal(access) signal(full) } consumer() { wait(full) wait(access) // Consume the q signal(access) signal(empty) } Note : Use signal NOT broadcast when using POSIX cond_wait if you know what i mean.