• ↑↓ to navigate
  • ↵ to open
  • ⟶ to select
  • ⌘ ⌥ ↵ to open in a panel
  • ←→ to navigate
  • esc to dismiss
⌘ '
keyboard shortcuts

The Problem

A producer generates data and puts it into a buffer, while a consumer takes data from the buffer. The buffer has limited size — producer must wait if full, consumer must wait if empty.

Core Idea

A classic synchronization problem solved using three semaphores: empty (counts free slots), full (counts filled slots), and mutex (protects buffer access).

How It Works

  1. Producer waits on empty semaphore (decrements)
  2. Producer waits on mutex (enters critical section)
  3. Producer adds item to buffer
  4. Producer signals mutex (exits critical section)
  5. Producer signals full (increments)
  6. Consumer does the reverse
prod_cons Prod Producer wait(empty) wait(mutex) Buf Buffer [N slots] Prod->Buf add item Cons Consumer wait(full) wait(mutex) Buf->Cons remove item

Key Properties

  • Uses 3 semaphores: empty (N), full (0), mutex (1)
  • Producer waits on empty, signals full
  • Consumer waits on full, signals empty
  • Mutex protects buffer data structure

Semantic Network

semantic_Producer_Consumer_Problem THIS Producer Consumer Pr REL1 Related Concept THIS--REL1 related REL2 Builds Into THIS--REL2 builds into

Connections

Edge Cases & Gotchas

  • Wrong semaphore order can cause deadlock (always mutex last in, first out)
  • Buffer must be protected by mutex during access
  • Can be extended to multiple producers/consumers