- C 91.9%
- Makefile 8.1%
|
|
||
|---|---|---|
| philo | ||
| LICENSE | ||
| README.md | ||
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_philosopherstime_to_die(in milliseconds)time_to_eattime_to_sleepnumber_of_times_each_philosopher_must_eat(optional argument)
For example, you can run the program like this:
-
./philo 2 200 20 20In this example, each philosopher's
time_to_dieexceeds the time required by the paramaterstime_to_eatandtime_to_sleep, so the program will run theoretically forever (you can stop the program anytime by pressingCTRL+C).
If you run the program with the following parameters:
-
./philo 1 100 10 10The only philosopher will die once its
time_to_dieis 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 5The 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
mansections 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.