Argument Format
The Philosophers program accepts 4 required arguments and 1 optional argument:The program will exit with an error if you provide fewer than 4 or more than 5 arguments.
Required Arguments
The number of philosophers sitting at the table (also the number of forks).Constraints:Special case: With 1 philosopher, the simulation will always result in death since only one fork is available.
- Must be a positive integer (greater than 0)
- Maximum value: 600 (defined by
PHILO_MAX) - Maximum digits: 9
Time in milliseconds before a philosopher dies from starvation.Constraints:In this example, if a philosopher doesn’t start eating within 410ms of their last meal, they die.Calculation tip: Should be greater than
- 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
time_to_eat to allow philosophers to survive.Time in milliseconds a philosopher spends eating.Constraints:Each eating session lasts exactly 200ms.Note: During eating, the philosopher’s two forks are unavailable to their neighbors.
- Must be a positive integer (greater than 0)
- Maximum digits: 9
- A philosopher holds two forks during this entire duration
Time in milliseconds a philosopher spends sleeping.Constraints:Each sleep session lasts exactly 150ms.Note: Philosophers do not hold forks while sleeping.
- Must be a positive integer (greater than 0)
- Maximum digits: 9
- Philosophers sleep after eating (before thinking)
Optional Arguments
The number of times each philosopher must eat before the simulation ends successfully.Constraints: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.
- Must be a positive integer (greater than 0)
- Maximum digits: 9
- If not provided, the simulation runs until a philosopher dies
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.,+5is valid) - No leading zeros beyond a single digit
- Maximum 9 digits per number
Error Messages
Example Configurations
Balanced Configuration
- 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
- 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
- Maximum realistic philosopher count
- Each must eat exactly 10 times
- Tests thread synchronization and mutex performance
Guaranteed Success
- 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 × 2Death Scenario Calculation
A philosopher dies if:last_meal_time is updated when the philosopher starts eating, not when they finish.
Minimum Viable Timing
Forn 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.