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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Single-slot Producer-Consumer Problem

Producer Code

A
slot = 1
item  = 0

while(true) { 
produceItem
down(slot)
fillSlot()
up(item) }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Single-slot Producer-Consumer Problem

Consumer Code

A
slot = 1
item  = 0

while(true) { 
down(item)
removeItem()
up(slot)
consumeItem }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly