Project 8: Reader-Writer Lock Implementation

Your own read-write lock implementation using only mutexes and condition variables.

Quick Reference

Attribute Value
Primary Language C
Alternative Languages N/A
Difficulty Level 4 (Expert)
Time Estimate See main guide
Knowledge Area Concurrency, Lock Design
Tooling See main guide
Prerequisites See main guide

What You Will Build

Your own read-write lock implementation using only mutexes and condition variables.

Why It Matters

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

Core Challenges

  • Writer starvation → Continuous readers block writers forever
  • Reader starvation → Giving writers priority starves readers
  • Fair queuing → FIFO ordering is complex

Key Concepts

  • Map the project to core concepts before you code.

Real-World Outcome

$ ./test_myrwlock --readers=10 --writers=3 --duration=10s

My RWLock Test (10 readers, 3 writers, 10 seconds)

Reader 1 acquired read lock (0 writers waiting)
Reader 2 acquired read lock (0 writers waiting)
Reader 3 acquired read lock (0 writers waiting)
Writer 1 waiting... (3 readers active)
Reader 1 released read lock
Reader 2 released read lock
Reader 3 released read lock
Writer 1 acquired write lock
Writer 1 released write lock
Reader 4 acquired read lock
...

Statistics:
  Read operations: 15,234
  Write operations: 456
  Max reader wait: 12ms
  Max writer wait: 45ms
  No starvation detected: PASS

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