Skip to content

xDec0de/42philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

194 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

42philosophers

42 project "philosophers", code follows the norm used at 42. The objective of this project is to implement a multithreaded simulation of the Dining Philosophers problem using POSIX threads (pthread). Each philosopher is represented by a thread, and forks are shared resources managed through mutexes to avoid concurrent access issues, built in C.

Some of the rules of this project:

  • No deadlocks occur.
  • No data races (Can be tested with the compilation flag fsanitize=thread)
  • Philosophers avoid starving. Which means they should try to avoid their own death.
  • Philosophers can't know if other philosopher is about to die.
  • Timing constraints (e.g., time to die, eat, sleep) are respected.
  • No memory leaks are present (Can be tested with valgrind)
  • The program doesn't crash under any circumstance.

Compilation

This project is compiled via Makefile

Command Description
make or make all Compiles updated files only
make re Recompiles the whole project (The src folder)
make clean Removes .o files (The objs folder)
make fclean make clean, also removing the program executable

Optional Compilation Variable

You can set the maximum number of philosophers (Default is 200) at compile time: make re MAX_PHILOSOPHERS=150. The program will still warn you if you go over 200 philosophers, but it will let you do it.

Usage

After compilation, run the executable ./philo with the following arguments:

./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [times_to_eat]
  • number_of_philosophers: Number of philosophers (threads).
  • time_to_die: Time (in milliseconds) a philosopher can go without eating.
  • time_to_eat: Time (in milliseconds) it takes to eat.
  • time_to_sleep: Time (in milliseconds) to sleep after eating.
  • times_to_eat (Optional): Simulation ends when each philosopher has eaten this many times.

For example, ./philo 5 800 200 200 3 launches 5 philosophers who will die if they don't eat within 800ms. They eat for 200ms, sleep for 200ms, and the simulation ends once each has eaten 3 times.

About

[42] An introduction to multithreading

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors