0

Philosophers — Multithreading & Synchronization

Philosophers is a concurrency project that simulates the classic Dining Philosophers problem using threads and mutexes. Built in C as part of the 42 Barcelona curriculum, it focuses on synchronization, deadlock avoidance, timing accuracy and safe shared-state management.

🧠 Philosophers — Concurrency & Synchronization

The Philosophers project is a multithreading simulation of the classic Dining Philosophers Problem, implemented entirely in C.
Its core objective is to manage shared resources safely using mutexes, avoid deadlocks, and ensure correct timing, all following the strict performance and memory standards of the 42 Barcelona curriculum.

This project strengthened my understanding of concurrency, thread management, and the challenges of synchronizing shared state.


šŸš€ Features

šŸ”¹ Multithreaded Simulation

Each philosopher runs in its own thread, performing:

  • thinking
  • eating
  • sleeping

With precise timestamp logging.

šŸ”¹ Mutex-Based Fork Management

  • Forks represented as mutexes
  • Prevents race conditions
  • Avoids simultaneous access to shared resources

šŸ”¹ Deadlock Prevention

Implements strategies to avoid circular waiting, ensuring philosophers can always progress.

šŸ”¹ Accurate Timing

Handles:

  • time_to_die
  • time_to_eat
  • time_to_sleep
  • microsecond-level accuracy

Using:

  • gettimeofday()
  • custom timing utilities

šŸ”¹ Robust Error Handling

  • Validates arguments
  • Gracefully handles invalid states
  • Ensures threads exit cleanly

🧠 What I Learned

  • Multithreading with pthread
  • Preventing deadlocks & starvation
  • Designing safe concurrent systems
  • Working with mutexes and shared resources
  • Real-time coordination and timing logic
  • Debugging race conditions and timing drifts

šŸ”— Repository

šŸ‘‰ GitHub: https://github.com/zmetreveli/philo