The following solution to the single producer single consumer problem uses…

PYQ GATE 2002

The following solution to the single producer single consumer problem uses semaphores for synchronization.

#define BUFFSIZE 100
buffer buf[BUFFSIZE];
int first=last=0;
semaphore b_full=0;
semaphore b_empty=BUFFSIZE;

void producer()
{
while (1) {
    produce an item;
    p1: …………………..;
    put the item into buff (first);
    first=(first+1)%BUFFSIZE;
    p2: …………………..;
}
}
void consumer()
{
while (1) {
    c1:……………………..
    take the item from buf[last];
    last=(last+1)%BUFFSIZE;
    c2: ……………………..;
    consume the item;
}
}

(a) Complete the dotted part of the above solution. (b) Using another semaphore variable, insert one line statement each immediately after p1, immediately before p2, immediately after c1, and immediately before c2 so that the program works correctly for multiple procedures and consumers.

Sign up free to check your answer

Sign up free

Explore the full course: Zero To Hero

Loading lesson…