Skip to content

About

trying to solve classic philosophers problem with C(hopefully they'll survive)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by zgahrama.


Description

Philosophers is a concurrency project based on the classic Dining Philosophers Problem, originally formulated by Edsger Dijkstra in 1965.

The simulation places N philosophers around a circular table. Each philosopher alternates between three states: thinking, eating, and sleeping. A fork is placed between every two adjacent philosophers, and a philosopher needs both the fork to their left and the fork to their right in order to eat.

The goal is to prevent two critical concurrency issues:

  • Data races — multiple threads reading/writing shared memory without synchronization.
  • Deadlocks — all philosophers picking up one fork simultaneously, causing the simulation to freeze.

Each philosopher runs in its own POSIX thread (pthread). Forks are protected by mutexes to ensure exclusive access. A dedicated monitoring thread continuously checks whether any philosopher has exceeded their time_to_die without eating, or whether all philosophers have eaten the required number of meals.

Key design choices

Problem Solution
Deadlock prevention Even-indexed philosophers pick up left fork first; odd-indexed pick up right fork first
Death detection A separate monitoring thread polls every 1 ms
Race-free output A dedicated print_mutex ensures log lines are never interleaved
Meal tracking Each philosopher owns a meal_mutex protecting last_meal_time and meals_eaten

Instructions

Requirements

  • A C compiler (cc / gcc / clang)
  • make
  • POSIX threads library (-lpthread, available on any Linux/macOS system)

Compilation

make

This produces the philo binary in the project root.

make clean   # remove object files
make fclean  # remove object files and the binary
make re      # fclean + compile fresh

Usage

./philo <number_of_philosophers> <time_to_die> <time_to_eat> <time_to_sleep> [number_of_times_each_philosopher_must_eat]
Argument Type Description
number_of_philosophers int Number of philosophers (and forks) at the table
time_to_die int (ms) Time in milliseconds before a philosopher starves if they haven't started eating
time_to_eat int (ms) Time in milliseconds a philosopher spends eating
time_to_sleep int (ms) Time in milliseconds a philosopher spends sleeping
number_of_times_each_philosopher_must_eat int (optional) If provided, the simulation ends once every philosopher has eaten at least this many times

Examples

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

# Same, but each philosopher must eat at least 7 times
./philo 5 800 200 200 7

# Edge case: a single philosopher can only grab one fork and will die
./philo 1 800 200 200

Output format

Each logged event follows the pattern:

<timestamp_in_ms> <philosopher_id> <action>

Possible actions:

  • has taken a fork
  • is eating
  • is sleeping
  • is thinking
  • died in vain.

Resources

Concurrency & the problem itself

Tutorials & walkthroughs

AI usage

Claude / ChatGPT were used during this project for the following tasks:

  • Understanding the problem: asking for plain-English explanations of deadlock scenarios and how mutex ordering prevents them.
  • Debugging data races: describing symptoms (e.g. intermittent wrong timestamps) and asking which shared variables were likely unprotected.
  • Verifying mutex logic: asking whether specific lock/unlock sequences could cause deadlocks or priority inversion.

AI was not used to write any .c or .h source file. All implementation decisions (thread design, lock ordering strategy, monitoring approach) were made independently and understood fully before coding.

About

trying to solve classic philosophers problem with C(hopefully they'll survive)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages