Development repo for 42cursus philosophers project
For further information about 42cursus and its projects, please refer to 42-Common-Core-Guide repo.
This is a project in daily use. If you want to see the project that i have been evaluated in 42school click in releases at the right side!
The aim of this project is understand the cre concepts envolving threads by solving the Dining philosophers problem.
C-Programming-Language-2nd-Edition (PDF)
For detailed information, refer to the subject of the project
๐ This project consists of coding basic C functions (see below), which are then compiled
into a library for use in other projects of the cursus.
The library is written in C language and thus requires the gcc
compiler and standard C libraries to run.
1. Compiling the philo
To compile the philosophers project, run:
$ cd path/to/philo && make
Before testing, it's essential to understand what each parameter in the command line means:
When running the philo
program, you'll use the following command structure:
$ ./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]
number_of_philosophers
: The number of philosophers in the simulation.time_to_die
(in ms): The maximum time a philosopher can go without eating. If this time passes, the philosopher dies.time_to_eat
(in ms): The time it takes for a philosopher to eat once they have both forks.time_to_sleep
(in ms): The time a philosopher spends sleeping after eating.number_of_times_each_philosopher_must_eat
(optional): The simulation stops when each philosopher has eaten at least this number of times. If this argument is omitted, the simulation runs indefinitely until a philosopher dies.
You can test the philosophers' behavior with various scenarios to ensure that the logic works as expected. Here are some examples and what you should observe:
1. ./philo 1 800 200 200
- Description: You have 1 philosopher, and their
time_to_die
is set to 800 ms, while thetime_to_eat
andtime_to_sleep
are both 200 ms. - Expected Behavior: The philosopher should not be able to eat, as there's only one philosopher and hence only one fork. The philosopher will die after 800 ms because they cannot acquire two forks to eat.
2. ./philo 5 800 200 200
- Description: There are 5 philosophers, and each has a
time_to_die
of 800 ms,time_to_eat
of 200 ms, andtime_to_sleep
of 200 ms. - Expected Behavior: No philosopher should die. They should continuously take turns eating and sleeping.
3. ./philo 5 800 200 200 7
- Description: There are 5 philosophers, and the simulation is set to stop when each philosopher has eaten at least 7 times.
- Expected Behavior: No philosopher should die. The simulation will stop when each philosopher has eaten 7 times.
4. ./philo 4 410 200 200
- Description: 4 philosophers with
time_to_die
set to 410 ms,time_to_eat
to 200 ms, andtime_to_sleep
to 200 ms. - Expected Behavior: No philosopher should die since there is enough time for each philosopher to eat before the
time_to_die
runs out.
5. ./philo 4 310 200 100
- Description: 4 philosophers with a
time_to_die
of 310 ms,time_to_eat
of 200 ms, andtime_to_sleep
of 100 ms. - Expected Behavior: One philosopher will die because the time left between eating and dying is too short for all the philosophers to complete the eating and sleeping cycles in time.
6. ./philo 2 <various times>
- Description: Use 2 philosophers with different
time_to_die
,time_to_eat
, andtime_to_sleep
values to test edge cases. - Expected Behavior: Test for accurate death timing. A death delayed by more than 10 ms is unacceptable in these tests.
- Do not test with more than 200 philosophers, as it may result in undefined behavior due to system limitations or constraints on thread management.
- Avoid setting
time_to_die
,time_to_eat
, ortime_to_sleep
to values lower than 60 ms, as such small time values could cause unexpected behavior due to the operating system's task scheduling limits.
- Ensure philosophers die at the correct time.
- Check that philosophers do not steal forks (i.e., no two philosophers can hold the same fork simultaneously).
- Verify that the time intervals between eating, sleeping, and dying match the expected values based on the input parameters.
By running these tests, you'll ensure that your simulation meets the projectโs requirements and behaves as expected under various scenarios.
I haven't written a custom tester yet, but you can make your own tests for each function. Here are some third-party testers that you can use:
These tools can help verify the correctness and performance of your philosopher simulation.