Semaphore roles despite reversed names
Synchronization & Concurrency

Answer & solution
Official answer: C
Method
Follow the definitions rather than familiar variable names. Before adding an item, the producer must reserve one writable slot, so it waits on the semaphore initially N. After insertion it creates a readable item, so it signals the semaphore initially 0. The consumer performs the reverse transfer. Waiting for a resource before taking mutex also avoids holding the buffer lock while blocked for another process to make progress. Each successful pair of operations preserves the bounded-buffer accounting.
Solution
Use the definitions supplied, which reverse the usual semaphore names. full starts N and counts producer-writeable slots; empty starts 0 and counts readable items. The producer waits(full) and signals(empty); the consumer waits(empty) and signals(full), each using mutex for buffer access. Thus P=full, Q=empty, R=empty, S=full, C.
![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)
![Question 20(b), 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(b): Introduce one additional semaphore and one statement at each location: immediately after p1, before p2, after c1, before c2 so the program supports multiple producers/consumers.](../../../assets/questions/CS-2002-Q96-1.webp)
![Question 20(b), 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(b): Introduce one additional semaphore and one statement at each location: immediately after p1, before p2, after c1, before c2 so the program supports multiple producers/consumers.](../../../assets/questions/CS-2002-Q96-2.webp)
![Question 20(b), 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(b): Introduce one additional semaphore and one statement at each location: immediately after p1, before p2, after c1, before c2 so the program supports multiple producers/consumers.](../../../assets/questions/CS-2002-Q96-3.webp)