• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

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

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