Code for Producer:
while (true) {
/* produce an item in nextProduced */
while (counter == BUFFER.SIZE) ;
/* do nothing */
wait(mutex);
buffer[in] = nextProduced;
in = (in + 1) % BUFFER-SIZE;
counter++;
signal(mutex);
}
Code for consumer process :
while (true) {
while (counter == 0) ; /* do nothing */
wait(mutex);
nextConsumed = buffer [out] ;
out = (out + 1) % BUFFER_SIZE;
counter--;
signal(mutex);
}
Can this be an alternative solution for bound buffer problem(where only one semaphore is used instead of three)?