Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

ReivenIV/42_Philosophers_dinner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⡫⠭⣽⠿⠿⠛⠻⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⠋⠀⣴⡟⠠⢃⠈⣃⠹⠃⡸⠀⠻⣎⠻⣿⣿Done⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣆⠀⢿⠹⣦⣝⣯⣶⣶⠋⠁⠂⡠⢏⣼⣿⣿⣿⣿⣿⣿by⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠙⣿⣛⡋⠀⣀⡴⠋⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿Philosophers⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢦⣄⠀⠉⠙⠒⠶⣾⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠈⣿⣷⣶⣶⣶⣶⣸⢠⣿⣿⣗⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⠧⠙⠿⣿⣿⠿⢋⣵⣿⣿⡿⣹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣷⣦⣤⣤⡄⠀⠀⠀⠀⣄⠘⢻⣿⡯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣔⠀⣶⣄⠸⣧⣬⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⡉⠁⠀⠀⠀⠀⠀⠉⠻⣿⣿⣿⠀⣿⣿⣿⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠘⣿⡇⠀⣿⣿⣟⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣷⣦⣄⡀⠀⠀⣀⣤⠻⡇⢸⣿⣿⣳⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⢸⣿⣿⣽⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⢾⣿⣷⣿⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿

Any fool can write code that a computer can understand.
Good programmers write code that humans can understand,
so if you want to go fast, if you want to get done quickly,
if you want your code to be easy to write, make it easy to read.

Martin Fowler && Robert C. Martin

GitHub code size in bytes

GitHub last commit

# Philosophers Project

Project validated : mars 2025 time spent : 44h 12min points : 100 / 100

Description

This project is a version of the Dining Philosophers Problem.

The goal of this project was to learn to avoid deadlock and race conditions when creating multithreaded applications.

The Philosophers project is a multi-threading and synchronization project aimed at simulating the classical dining philosophers problem. The goal is to implement a solution in which a set of philosophers seated at a round table engage in actions of thinking and eating while avoiding deadlock and starvation.

Requirements

To run the Philosophers project, you need:

  • A C compiler (such as GCC or Clang)
  • Basic understanding of concurrency and synchronization techniques
  • Familiarity with POSIX threads and mutexes

Installation

  1. Clone the repository:

    git clone <repository_url>
  2. Navigate to the project directory:

    cd philosophers_dinner
  3. Compile the program:

    make
    
    # or i like :
    rm philo | make re 

Usage

To execute the program, run the following command:

./philo [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [number_of_times_each_philosopher_must_eat]

Replace the placeholders with appropriate values:

  • [number_of_philosophers]: The number of philosophers seated at the table.
  • [time_to_die]: The time (in milliseconds) after which a philosopher dies if they haven't eaten.
  • [time_to_eat]: The time (in milliseconds) it takes for a philosopher to finish eating.
  • [time_to_sleep]: The time (in milliseconds) a philosopher spends sleeping after eating or thinking.
  • optional [number_of_times_each_philosopher_must_eat]: The number of times each philosopher must eat before the simulation ends.

Each philosopher logs the following actions:

  • Taking a fork
  • Eating
  • Sleeping
  • Thinking
  • Dying

The logged messages are always in sequential order.

Keep in mind before input data :

Thinking time or "t_think" is the contention time waiting for the forks If

Even amount of philosophers

- t_die = t_eat + t_sleep + 10

- if (t_eat <= t_sleep) === 0 t_think
	- No contention and the process is symetric philos will sleep and eat.
	- exs: 
		- ./philo 4 800 200 350
		- ./philo 4 800 200 200
- if (t_eat > t_sleep) ===  t_think = t_eat - t_sleep
		- ./philo 4 800 200 100
		- ./philo 4 800 200 60

Odd amount of philosophers :

- t_die = t_eat * 2 + t_sleep + 10

- if (t_eat = t_sleep) === t_think = t_eat 
	- ./philo 3 800 200 200
	- ./philo 3 800 100 100
- if (t_eat < t_sleep) === t_think = t_eat * 2 - t_sleep
	- ./philo 3 800 200 300
	- ./philo 3 800 200 350
	- ./philo 3 800 200 400

exec projet ex:

	# Normal launch
	./philo 1 800 200 200 			# (will die)
	./philo 5 800 200 200
	./philo 5 800 200 200 7
	./philo 4 410 200 200 
	./philo 4 310 200 100			# (one philo will die)

	# to check : Leaks
	valgrind ./philo 2 800000 60 60 1

	valgrind --tool=memcheck --leak-check=full --track-origins=yes --show-leak-kinds=all ./philo 2 800000 60 60 1

	# to check : Data races
	valgrind --tool=helgrind ./philo 2 800000 60 60 1
	valgrind --tool=helgrind --history-level=full -s ./philo 2 800000 60 60 1
	# data races complet report :
	valgrind --tool=helgrind --verbose --history-level=full -s ./philo 2 800000 60 60 1

usefull comands :

# clean vs-code cash
rm -rf ~/.config/Code/CachedData

# While the project is running you can check the proces (PID) data with these comand : 
ps aux | grep "path of your projet"

Contributing

Contributions to the project are welcome! If you have any ideas, enhancements, or bug fixes, feel free to open an issue or submit a pull request.

Ref proyects :

Tools

  • Usefull source to visualize the outputs. Be carefull is not perfect nor precice (check the issues) still a usefull tool: https://nafuka11.github.io/philosophers-visualizer/ i used in almost every test to understand the behavior of my project. And for the thinking behavior.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published