Similar questions 2 more →
Synchronization & Concurrency
![Question 20(a), GATE 2002: Circular buffer size 100, indices first=last=0, semaphores b_full=0,b_empty=100. Producer repeats produce item; p1 blank; put into buf[first]; first=(first+1)%100; p2 blank. Consumer repeats c1 blank; take from buf[last]; last=(last+1)%100; c2 blank; consume item. Original20(a): Fill p1,p2,c1,c2 for one producer and one consumer.](../../assets/questions/CS-2002-Q95-1.webp)
![Question 20(a), GATE 2002: Circular buffer size 100, indices first=last=0, semaphores b_full=0,b_empty=100. Producer repeats produce item; p1 blank; put into buf[first]; first=(first+1)%100; p2 blank. Consumer repeats c1 blank; take from buf[last]; last=(last+1)%100; c2 blank; consume item. Original20(a): Fill p1,p2,c1,c2 for one producer and one consumer.](../../assets/questions/CS-2002-Q95-2.webp)
![Question 20(a), GATE 2002: Circular buffer size 100, indices first=last=0, semaphores b_full=0,b_empty=100. Producer repeats produce item; p1 blank; put into buf[first]; first=(first+1)%100; p2 blank. Consumer repeats c1 blank; take from buf[last]; last=(last+1)%100; c2 blank; consume item. Original20(a): Fill p1,p2,c1,c2 for one producer and one consumer.](../../assets/questions/CS-2002-Q95-3.webp)
Answer & solution
Answer: p1: wait(b_empty); p2: signal(b_full); c1: wait(b_full); c2: signal(b_empty). (no official key was published)
Complete the source placeholders with:
p1: wait(b_empty);
p2: signal(b_full);
c1: wait(b_full);
c2: signal(b_empty);
The producer reserves an empty slot before writing buf[first] and advancing first, then announces a full slot. The consumer reserves a full slot before reading buf[last] and advancing last, then announces an empty slot. With b_empty initially BUFSIZE=100 and b_full initially zero, counting semaphores prevent overrun and underrun. Only one producer updates first and only one consumer updates last, so no additional mutex is needed for this single-producer/single-consumer algorithm under the usual atomic semaphore and memory-visibility assumptions. A semaphore count can exclude a reserved slot temporarily while its operation is in progress; b_empty+b_full need not equal BUFSIZE at every instruction boundary.