Project 7: Producer-Consumer with Mutexes and Condition Variables

The classic bounded-buffer producer-consumer with multiple producers and consumers using pthreads.

Quick Reference

Attribute Value
Primary Language C
Alternative Languages Rust, C++
Difficulty Level 3 (Advanced)
Time Estimate See main guide
Knowledge Area Concurrency, Synchronization
Tooling See main guide
Prerequisites See main guide

What You Will Build

The classic bounded-buffer producer-consumer with multiple producers and consumers using pthreads.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Spurious wakeups → Why while() not if()
  • Broadcast vs Signal → When to use each
  • Deadlock potential → Lock ordering matters

Key Concepts

  • Map the project to core concepts before you code.

Real-World Outcome

$ ./producer_consumer --producers=3 --consumers=2 --buffer-size=10 --items=1000

Producer-Consumer Simulation
Buffer size: 10, Producers: 3, Consumers: 2, Items: 1000

[P1] Produced item 1
[P2] Produced item 2
[C1] Consumed item 1
[P3] Produced item 3
[P1] Produced item 4
[C2] Consumed item 2
...
[C1] Consumed item 1000

Statistics:
  Total produced: 1000
  Total consumed: 1000
  Buffer empty waits: 234
  Buffer full waits: 156

PASS: All items produced and consumed correctly

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: UNIX_IPC_STEVENS_VOL2_MASTERY.md
  • Primary references are listed in the main guide