Producer-Consumer Synchronization Flashcards
1
Q
Single-slot Producer-Consumer Problem
Single-slot Producer-Consumer Problem
A
- One Slot: multiple producers and multiple consumers
- removeItem()
- consumeItem()
2
Q
Single-slot Producer-Consumer Problem
Producer Code
A
slot = 1 item = 0 while(true) { produceItem down(slot) fillSlot() up(item) }
3
Q
Single-slot Producer-Consumer Problem
Consumer Code
A
slot = 1 item = 0 while(true) { down(item) removeItem() up(slot) consumeItem }
4
Q
Producer-Consumer Problem
Producer-Consumer Problem
A
- Circular queue (fixed-size buffer)
- Producer: inserts items, blocked when queue is full
- Consumer: removes items, blocked when queue is empty
5
Q
Producer-Consumer Problem
Producer Code
A
slots = n items = 0 mutex = 1 down(slots) mutex_lock(mutex) buffer[pi] = data pi = (pi + 1) % n mutex_unlock(mutex) up(items)
6
Q
Producer-Consumer Problem
Consumer Code
A
slots = n items = 0 mutex = 1 down(items) mutex_lock(mutex) result = buffer[ci] ci = (ci + 1) % n mutex_unlock(mutex) up(slots)