School 42 Málaga project.
This repository contains all the files for the Philosophers project in the School 42 Málaga curriculum.
About · Requirements · Instructions · Testing · Tips · License
The Philosophers project is designed to test your knowledge of multithreading and synchronization in C. In this project, you will implement the dining philosophers problem, where a group of philosophers must compete for access to a limited number of resources (forks) to eat. You will need to ensure that the philosophers can eat without deadlocks or conflicts. You can find more information in the project subject.
The code in this repository follows the rules of the Norminette.
The project is written in C language and requires the following:
- Compiler
gcc
. - Standard libraries:
<stdlib.h>
<unistd.h>
<pthread.h>
To download the repository, open a terminal and run:
git clone --recursive https://github.com/Javisanchezf/42Malaga-philosophers.git
cd 42Malaga-philosophers
To compile the library, go to its path and run:
cd philo
make
Or you can compile the bonus part with:
cd philo_bonus
make
To delete all files generated with make, go to the path and run:
make fclean
To run the program, use the following command:
./philo [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [number_of_times_to_eat]
Replace the placeholders with the desired values for the simulation. For example:
./philo 5 800 200 200 7
- Use the sanitaze flag when compiling (-fsanitize=thread) or valgrind to see the datarace.
- Be careful with variables you access from different threads. Create a mutex for each variable that you can access if necessary.
- Be careful that they cannot speak at the same time, put a mutex protecting your printf.
- Be especially careful when it is 0 or 1 philosopher.
- Make the philosophers left/right handed so there are no conflicts when picking up the forks 😎 .
- Create your own sleep function so that the times are more exact according to the current time.
- CHALLENGE: Imagine that philosophers are sleepwalkers and the sleeping time is 0. Make sure your program doesn't hang.
To test the program, you can try different combinations of parameters and observe how the philosophers behave. Pay attention to how they compete for forks and whether they are able to eat without conflicts.
- IMPORTANT: For these tests compile the code only with the flags -Wall -Werror -Wextra -pthread. Since if you add Sanitize or Valgrind it will slow down the times and will not give exact results.
./philo 4 410 200 200 10 | grep "is eating" | wc -l
At least 40 must be obtained.
cc *.c -Wall -Werror -Wextra -pthread -fsanitize=thread -g -o philo_sanitize
./philo_sanitize 4 410 100 100 8
No Datarace notices should appear.
DEAD:
./philo 0 410 200 200 1
./philo 1 410 200 200 1
./philo 5 410 200 200 10
SURVIVE:
./philo 2 100 50 50 10
./philo 2 100 50 0 10
./philo 2 1 0 0 100
./philo 1 1 0 0 0
./philo 100 50 1000 1000 0
DEAD:
./philo 2 100 51 0 10
./philo 2 100 50 51 10
./philo 4 400 201 0 10
./philo 2 2 3 0 1
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
You are free to:
- Share: copy and redistribute the material in any medium or format.
- Adapt: remix, transform, and build upon the material.
Under the following terms:
- Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- NonCommercial: You may not use the material for commercial purposes.
- ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.