Skip to content

PedroZappa/42_philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

42 Project : philosophers

philosophers

Who thought treading on parallelism & concurrency land could be so philosophically deadly 🕱



Table o'Contents

About 📌

This is a classic CS exercise conceived as an introduction to the basics of threading a process. It gives an introductory glance into the difficulties contingent to the non-deterministic nature of multi-threaded applications.

philosophers Problem 🜎

  • One or more philosophers sit at a round table. There is a large bowl of spaghetti in the middle of the table.

  • The philosophers alternate between eating, thinking, or sleeping:

    • While they are eating, they are not thinking nor sleeping;
    • while thinking, they are not eating nor sleeping;
    • While sleeping, they are not eating nor thinking.
  • There are also forks on the table. There are as many forks as philosophers.

  • A philosopher must take two forks to eat, one in each hand.

  • When finished eating, they put their forks back on the table and start sleeping.

  • Once awake, they start thinking again.

  • The simulation stops when a philosopher dies of starvation.

  • Every philosopher needs to eat and should never starve.

  • Philosophers don’t speak with each other.

  • Philosophers don’t know if another philosopher is about to die.

philosophers Requirements Overview ✅

  • The program must not include any global variables.

  • It should accept the following command line arguments (as positive integers):

./philo <n_philos> <time_to_die> <time_to_eat> <time_to_sleep> [max_meals]
  • n_philos also the number of forks.

  • time_to_die the time to die in milliseconds (length of philo's life).

  • time_to_eat the time to eat in milliseconds (length of time to hold forks).

  • time_to_sleep the time to sleep in milliseconds (length of time to rest).

  • max_meals the maximum number of meals for the simulation (optional argument).

  • Each philosopher is numbered from 1 to n_philos.

  • Philosophers number 1 should be sitting next to philosopher n_philos and philosophers number 2.

Regarding logs, the program should report any state change of a philosopher formatted as:

  • timestamp_in_ms <philo_n> has taken a fork.

  • timestamp_in_ms <philo_n> is eating.

  • timestamp_in_ms <philo_n> is sleeping.

  • timestamp_in_ms <philo_n> is thinking.

  • timestamp_in_ms <philo_n> has died.

  • Messages should not mix with each other.

  • A message announcing the end of the simulation should be displayed no more than 10ms after the time of death of the philosopher.


philosophers Mandatory Implementation 📜

  • Each philosopher should be represented by a thread.

  • There is a fork between each pair of philosophers.

  • If there is only one philosopher, there should be only one fork on the table.

  • Each forks must be pretended by a mutex.


philosophers Bonus Implementation 📜

  • All forks are placed in the middle of the table.

  • They have no states in memory. The number of available forks is represented by a semaphore.

  • Each philosopher should be represented by a process, but the main process should not be a philosopher

Important

The code for both implementations is documented with doxygen comments. Enjoy!


Usage 🏁

Clone the repository and cd into either the mandatory (philo) or bonus implementation (philo_bonus):

git clone https://github.com/PedroZappa/42_philosophers.git 42_philosophers_passunca
# For the mandatory (Threads implementation w/ mutexes)
cd 42_philosophers_passunca/philo
# For the bonus (Processes implementation w/ semaphores)
cd 42_philosophers_passunca/philo_bonus

Build the program:

# For the mandatory
make

Run the program:

./philo 5 800 200 200
# or with a set max number of meals
./philo 5 800 200 200 7

Note

To execute the bonus implementation, you will need to instead call ./philo_bonus command.


Tests & Debug 🧪

Run the following command and look at the Test Rules 🧪 & Debug Rules  to get a comprehensive list of all available test/debug commands:

make help

Note

If you use tmux you are in for treat 😏


References 📚

Documentation

Research Papers


Tools

Articles


License 📖

This work is published under the terms of 42 Unlicense.

(get to top)