Overview

Philosophers simulates the classic dining philosophers problem where multiple philosophers sit around a table, alternating between eating, thinking, and sleeping. Each needs two forks (shared resources) to eat. The challenge is preventing deadlocks and data races.

What I Built

  • A multithreaded simulation using POSIX threads (pthread_create, pthread_join)
  • Mutex-based resource locking to prevent data races on shared forks
  • Death detection: a monitor thread checks if any philosopher has starved
  • Precise timing with usleep and gettimeofday for millisecond accuracy
  • Proper thread lifecycle management and graceful shutdown

Key Concepts

  • Thread synchronization with pthread_mutex_lock / pthread_mutex_unlock
  • Deadlock prevention strategies (fork ordering, odd/even scheduling)
  • Race conditions and why printf isn't thread-safe without protection
  • Real-time simulation with precise timing constraints
  • The difference between threads and processes (vs. the bonus fork() version)