In this project, we must solve the classic diner philosopher problem. Read the 42's subject for more details, or check the videos linked down below.
- Data Races: Occur when multiple tasks or threads access a shared resource without sufficient protections
- Mutex: Mutual Exclusion - Protect the execution again other threads running at the same time.
- Semaphore: Some kind of mutex, but with counter (Just bonus)
- Each philosopher is a thread and each fork is protected by a mutex.
- There are as many forks as philosophers.
- number_of_philosophers: The number of philosophers and also the number of forks.
- time_to_die: (in milliseconds): It's the max time that the philo must stay without eat, otherwise, he must die. To kill philo, we must calculate the last meal time with this time.
- time_to_eat: (in milliseconds): The time it takes for a philosopher to eat. The thread (or philo) will sleep until this time. During that time, the philo needs hold two forks.
- time_to_sleep: (in milliseconds): The time a philosopher will spend sleeping. The thread (or philo) will sleep until this time.
- number_of_times_each_philosopher_must_eat: (optional argument): If all philosophers have eaten at least this parametrized times, the simulation stops. If not specified, the simulation stops when a philosopher dies.
function | Description |
---|---|
usleep |
suspend execution for microsecond intervals |
gettimeofday |
sys/time.h - get / set time |
pthread_create |
pthread.h - Thread creation |
pthread_join |
pthread.h - function to wait for a thread to end. |
pthread_mutex_init / pthread_mutex_destroy |
pthread.h - destroy and initialize a mutex |
pthread_mutex_unlock / pthread_mutex_lock |
pthread.h - lock and unlock a mutex |
- Valgrind with warning DataRace: Turn on flag:
-fsanitize=thread
. - Valgrind with helgrind: Found thread errors: execute
make helgrind
- to run:
make run
(Down bellow has more tests with different params)
- Test covered by: Philosphers-42Project-Tester
# in root folder:
$ git clone https://github.com/newlinuxbot/Philosphers-42Project-Tester.git
$ cd Philosphers-42Project-Tester
$ ./start.sh ../ 1
# How it's working with threads, the result must be different in some machines.
#The test was instable in my machine. Was successfully in 42 intranet.
- Turn on / uncomment cflags in makefile, with PRETTY flag to get a better visualization result:
# Test 1: Philo Should not eat and should die
$ ./philo 1 800 200 200
# Test 2: No philo should die
$ ./philo 5 800 200 200
# Test 3: No philo should die and dining stop when each eat at least 7x
$ ./philo 5 800 200 200 7
$ ./philo 5 800 200 200 7 | grep 'is eating'
# Test4: No philo should die
$ ./philo 4 410 200 200
# Test5: One philo should die
$ ./philo 4 310 200 100
# Test6: 2 philos. Time to death, lower than 10ms
$ ./philo 2 310 200 100
##
##Random tests:
##
$ ./philo 3 8100 2000 2000 2
$ ./philo 2 4100 2000 2000 2
$ ./philo 4 410 200 200 5
$ ./philo 4 410 200 200
$ ./philo 2 800 200 200 2
#Kill philos
$ ./philo 2 60 60 60
$ ./philo 4 310 200 100