Skip to content

HazeltheJ/philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🍝 Philosophers

The classic dining philosophers problem β€” solved with threads and mutexes

C 42 Status


πŸ“– About

Philosophers is a 42 project that tackles the classic dining philosophers problem using threads and mutexes. The real challenge is managing concurrent access to shared resources without causing deadlocks or data races. Each philosopher must think, eat, and sleep in their own thread while competing for forks with their neighbors β€” all without starving to death. Proper synchronization is critical, and timing precision determines whether philosophers live or die.


✨ Features

  • Multi-threaded simulation with pthread
  • Mutex-based synchronization for shared resources (forks)
  • Deadlock prevention through careful resource ordering
  • Precise timing with usleep() and gettimeofday()
  • Real-time status logging (thinking, eating, sleeping, dying)
  • Configurable parameters: number of philosophers, time to die/eat/sleep

πŸš€ Usage

git clone https://github.com/YourName/philosophers.git
cd philosophers
make
./philo [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [optional: number_of_times_each_philosopher_must_eat]

Example:

./philo 5 800 200 200
# 5 philosophers, 800ms to die, 200ms to eat, 200ms to sleep

🧠 Key Concepts

  • Threading β€” pthread_create(), pthread_join(), pthread_detach()
  • Mutexes β€” pthread_mutex_lock(), pthread_mutex_unlock()
  • Race conditions β€” protecting shared data from concurrent access
  • Deadlock avoidance β€” preventing circular wait conditions
  • Timing precision β€” microsecond-level accuracy for survival checks

No philosophers were harmed in the making of this project

About

🍝 The dining philosophers problem solved with `threads` and `mutexes` β€” deadlock-free synchronization in C. 42 School.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors