Skip to main content

Argument Format

The Philosophers program accepts 4 required arguments and 1 optional argument:
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]
The program will exit with an error if you provide fewer than 4 or more than 5 arguments.

Required Arguments

number_of_philosophers
integer
required
The number of philosophers sitting at the table (also the number of forks).Constraints:
  • Must be a positive integer (greater than 0)
  • Maximum value: 600 (defined by PHILO_MAX)
  • Maximum digits: 9
Example:
./philo 5 800 200 200
Special case: With 1 philosopher, the simulation will always result in death since only one fork is available.
time_to_die
integer
required
Time in milliseconds before a philosopher dies from starvation.Constraints:
  • Must be a positive integer (greater than 0)
  • Maximum digits: 9
  • Measured from the start of the simulation or the last time the philosopher started eating
Example:
./philo 4 410 200 200
In this example, if a philosopher doesn’t start eating within 410ms of their last meal, they die.Calculation tip: Should be greater than time_to_eat to allow philosophers to survive.
time_to_eat
integer
required
Time in milliseconds a philosopher spends eating.Constraints:
  • Must be a positive integer (greater than 0)
  • Maximum digits: 9
  • A philosopher holds two forks during this entire duration
Example:
./philo 4 800 200 200
Each eating session lasts exactly 200ms.Note: During eating, the philosopher’s two forks are unavailable to their neighbors.
time_to_sleep
integer
required
Time in milliseconds a philosopher spends sleeping.Constraints:
  • Must be a positive integer (greater than 0)
  • Maximum digits: 9
  • Philosophers sleep after eating (before thinking)
Example:
./philo 5 800 200 150
Each sleep session lasts exactly 150ms.Note: Philosophers do not hold forks while sleeping.

Optional Arguments

number_of_times_each_philosopher_must_eat
integer
The number of times each philosopher must eat before the simulation ends successfully.Constraints:
  • Must be a positive integer (greater than 0)
  • Maximum digits: 9
  • If not provided, the simulation runs until a philosopher dies
Example:
./philo 5 800 200 200 7
The simulation ends successfully when all 5 philosophers have eaten 7 times each.Behavior: When all philosophers reach the meal count, the program terminates without any “died” message.

Input Validation

The program performs strict validation on all arguments:

Valid Input Format

  • Positive integers only (no negative numbers)
  • Optional + prefix is allowed (e.g., +5 is valid)
  • No leading zeros beyond a single digit
  • Maximum 9 digits per number

Error Messages

$ ./philo 4 800 200
Invalid number of arguments

Example Configurations

Balanced Configuration

./philo 5 800 200 200
  • 5 philosophers
  • 800ms to die (enough time for one eating cycle)
  • 200ms eating time
  • 200ms sleeping time
  • No meal limit (runs until death)

Testing Edge Cases

./philo 4 310 200 100
  • Tight timing: philosophers have only 310ms to eat again
  • With 200ms eating + 100ms sleeping = 300ms
  • Only 10ms margin for thinking and fork acquisition

Large Scale Simulation

./philo 200 800 200 200 10
  • Maximum realistic philosopher count
  • Each must eat exactly 10 times
  • Tests thread synchronization and mutex performance

Guaranteed Success

./philo 2 800 200 200 5
  • Only 2 philosophers (minimal contention)
  • Generous time_to_die (800ms)
  • Limited meals (5 per philosopher)
  • Should complete successfully

Parameter Relationships

For a successful simulation (no deaths), ensure:time_to_die > time_to_eat + [time for fork acquisition]A safe rule of thumb: time_to_die ≥ time_to_eat × 2

Death Scenario Calculation

A philosopher dies if:
current_time - last_meal_time ≥ time_to_die
The last_meal_time is updated when the philosopher starts eating, not when they finish.

Minimum Viable Timing

For n philosophers sharing forks in a circular arrangement:
  • Best case: time_to_die = time_to_eat + margin
  • Safe configuration: time_to_die = time_to_eat × 2
  • Conservative: time_to_die = time_to_eat + time_to_sleep + margin
The program validates inputs at startup (via parse_input.c) before initializing any threads or mutexes.

Build docs developers (and LLMs) love