GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Place slot-count and mutex operations around buffer transfers

3 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Each uses a bounded producer-consumer buffer and completes semaphore operations around insertion/removal.

2 marks · MCQ
Report issue

Semaphore roles despite reversed names

Synchronization & Concurrency

Question 40, GATE 2018: A size-N buffer uses empty=0, full=N, mutex=1. Here empty counts readable items and full writable slots. Producer executes wait(P),wait(mutex),add,signal(mutex),signal(Q); consumer wait(R),wait(mutex),remove,signal(mutex),signal(S). Choose A full/full/empty/empty; B empty/empty/full/full; C full/empty/empty/full; D empty/full/full/empty.
Tap to enlarge
Your answer
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.

Original paper, page 17 · Answer key

Descriptive
Report issue

Single-producer single-consumer semaphore blanks

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.
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.
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.
Tap to enlarge
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.

Original paper, page 10

Descriptive
Report issue

Mutex additions for multiple producers and consumers

Synchronization & Concurrency

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.
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.
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.
Tap to enlarge
Answer & solution

Answer: Initialize semaphore mutex=1; after p1 and c1 insert wait(mutex); before p2 and c2 insert signal(mutex). (no official key was published)

Add a binary semaphore mutex initialized to one. Insert wait(mutex) immediately after p1 and immediately after c1, and signal(mutex) immediately before p2 and immediately before c2. The resulting operation order is:
Producer: wait(b_empty); wait(mutex); write buf[first]; advance first; signal(mutex); signal(b_full).
Consumer: wait(b_full); wait(mutex); read buf[last]; advance last; signal(mutex); signal(b_empty).
This serializes updates to the shared buffer and indices across all producers and consumers. Slot availability is awaited before taking the mutex. Reversing that order can deadlock, for example a consumer holding the mutex while waiting for an item prevents a producer from inserting one. Producing and consuming the actual item stay outside the mutex-protected buffer operations.

Original paper, page 10