No description
  • C 91.9%
  • Makefile 8.1%
Find a file
2026-04-13 15:24:20 +01:00
philo Timestamp order was probably right all along, but still added something more correct. 2026-04-13 14:44:01 +01:00
LICENSE Initial commit 2026-01-19 15:38:43 +00:00
README.md Added README formatted according to subject 2026-04-01 16:08:16 +01:00

This project has been created as part of the 42 curriculum by grui-ant.

🍽️ 42-Philosophers 💭

A study into the art of avoiding Race Conditions, written in C.

"Je pense, donc je suis." - René Descartes, in Discours de la Méthode

Description

This project's main goal is to introduce one to the concept of multithreaded applications.
The program will spawn philosophers, which are represented by threads. Each philosopher must do the following, sequencially and in this order:

  • Eat;
  • Think;
  • Sleep;

The philosophers will repeat this routine for as long as the program runs, which depends on a handful of variables.

Instructions

Building

You will need to compile this program yourself, as no binaries are provided.
This project was was made with Ubuntu 22.04 as the target platform, so this guide will assume you are on Ubuntu.
However, you should be able to install the corresponding packages and build it on your distro of choice.

Prequisites:

Install the following dependencies:

  • sudo apt install clang gcc git libxext-dev libbsd-dev make xorg zlib1g-dev

Clone this repository using Git, cd into it, and pull all dependencies:

  • git clone https://git.the-wired.org/TreezZ/42-Philosophers.git && cd 42-Philosophers/philo

Once that's done, run GNU Make to build the source code into a executable binary:

  • make

The make clean and make fclean flags are also available to clean up the files created during compilation, as well as the binary, from the repository.

Running

To run the program, you will need to run it from the command line. It requires the following parameters to run:

  • number_of_philosophers
  • time_to_die (in milliseconds)
  • time_to_eat
  • time_to_sleep
  • number_of_times_each_philosopher_must_eat (optional argument)

For example, you can run the program like this:

  • ./philo 2 200 20 20

    In this example, each philosopher's time_to_die exceeds the time required by the paramaters time_to_eat and time_to_sleep, so the program will run theoretically forever (you can stop the program anytime by pressing CTRL + C).

If you run the program with the following parameters:

  • ./philo 1 100 10 10

    The only philosopher will die once its time_to_die is reached, because there are only as many forks as there are philosophers, and philosophers need 2 forks to eat.

Lastly, if you set the program to run like this:

  • ./philo 10 200 20 20 5

    The program will run until all philosophers have eaten at least 5 meals, and exit.

Go ahead and test multiple combinations to your heart's content!

Resources

  • The man sections found on a GNU/Linux Operating System;
  • The help of my colleagues;
  • All the other joys of peer-to-peer learning.

No so called "Artificial Intelligence" (LLM's, etc) was used in any phase of this project's development.
This includes information gathering, writing the code itself, debugging, etc.

License

All code in this repository is licensed under the terms of the GNU GPL-2.0-or-later.