I am trying to show that the following solution of the producer/consumer problem does not work, by showing that when a consumer is at the beginning of M1, there is a case when it won't be able to dequeue an item within a finite time, and/or there is a case when a producer is at the beginning of L2, and it will not be able to enqueue an item within a finite time. I just can't find any example to prove it.
The algorithm assumes there are 10 producers, 10 consumers, and a buffer size of 10.
nf = 0; // counting semaphore, # of items in queue
bm = 1; // binary semaphore, ensures mutex
Producer
L1: Produce(item);
L2: P(bm);
If (queue_is_full) {
V(bm);
GoTo L2;
} else {
Enqueue(item);
V(bm);
V(nf);
GoTo L1;
}
Consumer
M1: P(nf);
P(bm);
Dequeue(item);
V(bm);
Consume(item);
GoTo M1;