An open API service indexing awesome lists of open source software.

https://github.com/librity/ft_philosophers

42 São Paulo - Philosophers
https://github.com/librity/ft_philosophers

42 42born2code 42cursus 42projects 42saopaulo 42school c philosophers philosophers-dinner-problem philosophers42 pthreads threads

Last synced: 6 months ago
JSON representation

42 São Paulo - Philosophers

Awesome Lists containing this project

README

          

42 São Paulo - Philosophers

![42 São Paulo](https://img.shields.io/badge/42-SP-1E2952)
![License](https://img.shields.io/github/license/librity/ft_philosophers?color=yellow)
![Code size in bytes](https://img.shields.io/github/languages/code-size/librity/ft_philosophers?color=blue)
![Lines of code](https://img.shields.io/tokei/lines/github/librity/ft_philosophers?color=blueviolet)
![Top language](https://img.shields.io/github/languages/top/librity/ft_philosophers?color=ff69b4)
![Last commit](https://img.shields.io/github/last-commit/librity/ft_philosophers?color=orange)

[![Build](https://github.com/librity/ft_philosophers/actions/workflows/build.yml/badge.svg)](https://github.com/librity/ft_philosophers/actions/workflows/build.yml)
[![Norminette v3](https://github.com/librity/ft_philosophers/actions/workflows/norminette_v3.yml/badge.svg)](https://github.com/librity/ft_philosophers/actions/workflows/norminette_v3.yml)

An age-old threaded problem in pure C.


---

## 📜 Table of Contents

- [🧐 About](#about)
- [✅ Checklist](#checklist)
- [🏁 Getting Started](#getting_started)
- [📝 Notes](#notes)
- [🛸 42 São Paulo](#ft_sp)
- [📚 Resources](#resources)

## 🧐 About

A C implementation of the classic
[Dining Philosophers Problem](https://wikiless.org/wiki/Dining_philosophers_problem?lang=en)
by [Edsger Dijkstra](https://wikiless.org/wiki/Edsger_W._Dijkstra?lang=en),
with threads, mutexes, forks and semaphores.

## ✅ Checklist

- [x] Makefile should explicitly name all source files (`make dump_sources`).
- [x] Make must compile without relinking
- [x] `make all` shouldn't recompile/rearchive any objects or sources.
- [x] Add `.keep` to object dirs
- [x] Compiles with workspace's `cc` (`clang` version `12.0.1`)
- [x] Switch Makefile's `clang-12` to `CC` before submitting.
- [x] Test in workspaces
- [x] Written in C.
- [x] Follows `norminette 3.3.51`
- [x] Should not quit unexpectedly (segmentation fault, bus error, double free, etc.)
- [x] All allocated heap memory properly freed, no memory leaks.
- [x] Use gcc `-fsanitize=leak` flag.
- [x] Check memory leaks with `valgrind`.
- [x] Compiles with `-Wall -Wextra -Werror`
- [x] No global variables.
- [x] Receive 4-5 arguments.
- [x] `number_of_philosophers`: number of philosophers and forks.
- [x] `time_to_die` (ms)
- [x] Philosophers should die if they didn't eat since the BEGINNING of their last meal/beggining of the simulation.
- [x] `time_to_eat` (ms)
- [x] `time_to_sleep` (ms)
- [x] `number_of_times_each_philosopher_must_eat` (OPTIONAL)
- [x] Index philosophers from 1 to `number_of_philosophers`.
- [x] Program log:
- [x] `timestamp_in_ms X has taken a fork`
- [x] `timestamp_in_ms X is eating`
- [x] `timestamp_in_ms X is sleeping`
- [x] `timestamp_in_ms X is thinking`
- [x] `timestamp_in_ms X died`
- [x] Log messages shouldn't mix up with each other.
- [x] Death message should print before 10 ms.
- [x] Philosophers should avoid dying.
- [x] No data races.

### Mandatory

- [x] Program name `philo`
- [x] Turn in `Makefile`, `*.h`, `*.c` , `.gitignore` in directory `philo/`
- [x] Makefile rules: `$(NAME)` `all` `clean` `fclean` `re`
- [x] Allowed functions:
- [x] `memset` `printf` `malloc` `free` `write` `usleep`
- [x] `gettimeofday` `pthread_create` `pthread_detach`
- [x] `pthread_join` `pthread_mutex_init` `pthread_mutex_destroy`
- [x] `pthread_mutex_lock` `pthread_mutex_unlock`
- [x] `libft` FORBIDDEN
- [x] Philosophers alternatively `eat`, `sleep` or `think` (in that order)
- [x] Should calculate if philosopher will die and die.
- [x] There are as many forks as philosophers
- [x] Philosophers need to grab `left_fork` and `right_fork` before they can eat.
- [x] Create a thread for each philosopher.
- [x] Deal with lone philosopher separately.
- [x] Create a mutex for each fork.
- [x] Program ends when any one philosopher dies from starvation
- [x] Program ends when each philosopher ate `number_of_times_each_philosopher_must_eat`
- [x] Guardian Thread:
- [x] Checks whether any philosopher died/should be dead.
- [x] Check if everyone ate their last meal.
- [x] Polling + Mutex
- [x] Pass all testers
- [x]
- [x]
- [x]
- [x]

### Bonus

- [x] Program name `philo_bonus`
- [x] Turn in `Makefile`, `*.h`, `*.c` , `.gitignore` in directory `philo_bonus/`
- [x] Makefile rules: `$(NAME)` `all` `clean` `fclean` `re`
- [x] Allowed functions:
- [x] `memset` `printf` `malloc` `free` `write` `fork` `kill` `exit`
- [x] `pthread_create` `pthread_detach` `pthread_join` `usleep` `gettimeofday`
- [x] `waitpid` `sem_open` `sem_close` `sem_post` `sem_wait` `sem_unlink`
- [x] `libft` FORBIDDEN
- [x] Create a process for each philosopher.
- [x] Create a semaphore the forks.

## 🏁 Getting Started

### 📦 Dependencies

You will need `git`, `make` and `clang-12`:

```bash
$ sudo apt-get install git make clang-12
```

### 🖥️ Installing

Clone the repo and build with `make`:

```bash
$ git clone --recurse-submodules https://github.com/librity/ft_philosophers.git
$ cd ft_philosophers/philo
$ make
```

This should create a `philo` executable in the root folder:

```bash
./philo
```

## 📝 Notes

### Helgrind error: `lock order "0x4AAB040 before 0x4AAB068" violated`

![Potential Deadlock Diagram](./.github/potential_deadlock.png)
![Impossible Deadlock Diagram](./.github/impossible_deadlock.png)

## 🛸 42 São Paulo

Part of the larger [42 Network](https://www.42.fr/42-network/),
[42 São Paulo](https://www.42sp.org.br/) is a software engineering school
that offers a healthy alternative to traditional education:

- It doesn't have any teachers and classes.
- Students learn by cooperating
and correcting each other's work (peer-to-peer learning).
- Its focus is as much on social skills as it is on technical skills.
- It's completely free to anyone that passes its selection process -
[**The Piscine**](https://42.fr/en/admissions/42-piscine/)

It's an amazing school, and I'm grateful for the opportunity.

## 📚 Resources

### `memset()`

-

### `printf()`

-

### `malloc()`

-

### `free()`

-

### `write()`

-

### `usleep()`

-

### `gettimeofday()`

-
-
-
-

### `pthread_create()`

-

### `pthread_detach()`

-

### `pthread_join()`

-

### `pthread_mutex_init()`

-

### `pthread_mutex_destroy()`

-

### `pthread_mutex_lock()`

-
-

### `pthread_mutex_unlock()`

-

### `sem_open()`

-

### `sem_close()`

-

### `sem_post()`

-

### `sem_wait()`

-

### `sem_unlink()`

-

### `pthread.h`

-
-
-

### Data Races

-
-

### Atomic Types

-
-

### `semaphore.h`

-
-
-

### Tutorials

-
-
-

### Boards

-
-
-

### C Quirks

-

### Valgrind

-
-
-

### Helgrind `Lock Order Violated` Error

-
-

### `nice`

-

### Visualizers

-

### Testers

-
-
-
-
-

### Videos

-
-
-
-
-
-
-
-
-
-
-
-

### Python Multithreading

-