{"id":19483543,"url":"https://github.com/librity/ft_philosophers","last_synced_at":"2026-02-20T01:02:02.205Z","repository":{"id":99703207,"uuid":"553877015","full_name":"librity/ft_philosophers","owner":"librity","description":"42 São Paulo - Philosophers","archived":false,"fork":false,"pushed_at":"2022-11-29T01:12:52.000Z","size":321,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-05T11:51:16.402Z","etag":null,"topics":["42","42born2code","42cursus","42projects","42saopaulo","42school","c","philosophers","philosophers-dinner-problem","philosophers42","pthreads","threads"],"latest_commit_sha":null,"homepage":"https://www.42sp.org.br/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/librity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-18T23:08:49.000Z","updated_at":"2023-07-25T11:29:51.000Z","dependencies_parsed_at":"2023-07-31T12:00:40.955Z","dependency_job_id":null,"html_url":"https://github.com/librity/ft_philosophers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/librity/ft_philosophers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/librity%2Fft_philosophers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/librity%2Fft_philosophers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/librity%2Fft_philosophers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/librity%2Fft_philosophers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/librity","download_url":"https://codeload.github.com/librity/ft_philosophers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/librity%2Fft_philosophers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29637917,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T22:32:43.237Z","status":"ssl_error","status_checked_at":"2026-02-19T22:32:38.330Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["42","42born2code","42cursus","42projects","42saopaulo","42school","c","philosophers","philosophers-dinner-problem","philosophers42","pthreads","threads"],"created_at":"2024-11-10T20:15:30.507Z","updated_at":"2026-02-20T01:02:02.168Z","avatar_url":"https://github.com/librity.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch3 align=\"center\"\u003e42 São Paulo - Philosophers\u003c/h3\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n![42 São Paulo](https://img.shields.io/badge/42-SP-1E2952)\n![License](https://img.shields.io/github/license/librity/ft_philosophers?color=yellow)\n![Code size in bytes](https://img.shields.io/github/languages/code-size/librity/ft_philosophers?color=blue)\n![Lines of code](https://img.shields.io/tokei/lines/github/librity/ft_philosophers?color=blueviolet)\n![Top language](https://img.shields.io/github/languages/top/librity/ft_philosophers?color=ff69b4)\n![Last commit](https://img.shields.io/github/last-commit/librity/ft_philosophers?color=orange)\n\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![Build](https://github.com/librity/ft_philosophers/actions/workflows/build.yml/badge.svg)](https://github.com/librity/ft_philosophers/actions/workflows/build.yml)\n[![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)\n\n\u003c!-- [![Socrates Tester](https://github.com/librity/ft_philosophers/actions/workflows/socrates.yml/badge.svg)](https://github.com/librity/ft_philosophers/actions/workflows/socrates.yml) --\u003e\n\n\u003c!-- [![Tests](https://github.com/librity/ft_philosophers/actions/workflows/tests.yml/badge.svg)](https://github.com/librity/ft_philosophers/actions/workflows/tests.yml) --\u003e\n\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e An age-old threaded problem in pure C.\n  \u003cbr\u003e\n\u003c/p\u003e\n\n---\n\n## 📜 Table of Contents\n\n- [🧐 About](#about)\n- [✅ Checklist](#checklist)\n- [🏁 Getting Started](#getting_started)\n- [📝 Notes](#notes)\n- [🛸 42 São Paulo](#ft_sp)\n- [📚 Resources](#resources)\n\n## 🧐 About \u003ca name = \"about\"\u003e\u003c/a\u003e\n\nA C implementation of the classic\n[Dining Philosophers Problem](https://wikiless.org/wiki/Dining_philosophers_problem?lang=en)\nby [Edsger Dijkstra](https://wikiless.org/wiki/Edsger_W._Dijkstra?lang=en),\nwith threads, mutexes, forks and semaphores.\n\n## ✅ Checklist \u003ca name = \"checklist\"\u003e\u003c/a\u003e\n\n- [x] Makefile should explicitly name all source files (`make dump_sources`).\n- [x] Make must compile without relinking\n  - [x] `make all` shouldn't recompile/rearchive any objects or sources.\n  - [x] Add `.keep` to object dirs\n- [x] Compiles with workspace's `cc` (`clang` version `12.0.1`)\n  - [x] Switch Makefile's `clang-12` to `CC` before submitting.\n- [x] Test in workspaces\n- [x] Written in C.\n- [x] Follows `norminette 3.3.51`\n- [x] Should not quit unexpectedly (segmentation fault, bus error, double free, etc.)\n- [x] All allocated heap memory properly freed, no memory leaks.\n  - [x] Use gcc `-fsanitize=leak` flag.\n  - [x] Check memory leaks with `valgrind`.\n- [x] Compiles with `-Wall -Wextra -Werror`\n- [x] No global variables.\n- [x] Receive 4-5 arguments.\n  - [x] `number_of_philosophers`: number of philosophers and forks.\n  - [x] `time_to_die` (ms)\n    - [x] Philosophers should die if they didn't eat since the BEGINNING of their last meal/beggining of the simulation.\n  - [x] `time_to_eat` (ms)\n  - [x] `time_to_sleep` (ms)\n  - [x] `number_of_times_each_philosopher_must_eat` (OPTIONAL)\n- [x] Index philosophers from 1 to `number_of_philosophers`.\n- [x] Program log:\n  - [x] `timestamp_in_ms X has taken a fork`\n  - [x] `timestamp_in_ms X is eating`\n  - [x] `timestamp_in_ms X is sleeping`\n  - [x] `timestamp_in_ms X is thinking`\n  - [x] `timestamp_in_ms X died`\n- [x] Log messages shouldn't mix up with each other.\n- [x] Death message should print before 10 ms.\n- [x] Philosophers should avoid dying.\n- [x] No data races.\n\n### Mandatory\n\n- [x] Program name `philo`\n- [x] Turn in `Makefile`, `*.h`, `*.c` , `.gitignore` in directory `philo/`\n- [x] Makefile rules: `$(NAME)` `all` `clean` `fclean` `re`\n- [x] Allowed functions:\n  - [x] `memset` `printf` `malloc` `free` `write` `usleep`\n  - [x] `gettimeofday` `pthread_create` `pthread_detach`\n  - [x] `pthread_join` `pthread_mutex_init` `pthread_mutex_destroy`\n  - [x] `pthread_mutex_lock` `pthread_mutex_unlock`\n  - [x] `libft` FORBIDDEN\n- [x] Philosophers alternatively `eat`, `sleep` or `think` (in that order)\n- [x] Should calculate if philosopher will die and die.\n- [x] There are as many forks as philosophers\n- [x] Philosophers need to grab `left_fork` and `right_fork` before they can eat.\n- [x] Create a thread for each philosopher.\n  - [x] Deal with lone philosopher separately.\n- [x] Create a mutex for each fork.\n- [x] Program ends when any one philosopher dies from starvation\n- [x] Program ends when each philosopher ate `number_of_times_each_philosopher_must_eat`\n- [x] Guardian Thread:\n  - [x] Checks whether any philosopher died/should be dead.\n  - [x] Check if everyone ate their last meal.\n  - [x] Polling + Mutex\n- [x] Pass all testers\n  - [x] \u003chttps://github.com/GOAT095/philosophers-tester\u003e\n  - [x] \u003chttps://github.com/nesvoboda/socrates\u003e\n  - [x] \u003chttps://github.com/wwwwelton/philosophers/blob/master/test.sh\u003e\n  - [x] \u003chttps://pastebin.pl/view/85d80008\u003e\n\n### Bonus\n\n- [x] Program name `philo_bonus`\n- [x] Turn in `Makefile`, `*.h`, `*.c` , `.gitignore` in directory `philo_bonus/`\n- [x] Makefile rules: `$(NAME)` `all` `clean` `fclean` `re`\n- [x] Allowed functions:\n  - [x] `memset` `printf` `malloc` `free` `write` `fork` `kill` `exit`\n  - [x] `pthread_create` `pthread_detach` `pthread_join` `usleep` `gettimeofday`\n  - [x] `waitpid` `sem_open` `sem_close` `sem_post` `sem_wait` `sem_unlink`\n  - [x] `libft` FORBIDDEN\n- [x] Create a process for each philosopher.\n- [x] Create a semaphore the forks.\n\n## 🏁 Getting Started \u003ca name = \"getting_started\"\u003e\u003c/a\u003e\n\n### 📦 Dependencies\n\nYou will need `git`, `make` and `clang-12`:\n\n```bash\n$ sudo apt-get install git make clang-12\n```\n\n### 🖥️ Installing\n\nClone the repo and build with `make`:\n\n```bash\n$ git clone --recurse-submodules https://github.com/librity/ft_philosophers.git\n$ cd ft_philosophers/philo\n$ make\n```\n\nThis should create a `philo` executable in the root folder:\n\n```bash\n./philo\n```\n\n## 📝 Notes \u003ca name = \"notes\"\u003e\u003c/a\u003e\n\n### Helgrind error: `lock order \"0x4AAB040 before 0x4AAB068\" violated`\n\n![Potential Deadlock Diagram](./.github/potential_deadlock.png)\n![Impossible Deadlock Diagram](./.github/impossible_deadlock.png)\n\n## 🛸 42 São Paulo \u003ca name = \"ft_sp\"\u003e\u003c/a\u003e\n\nPart of the larger [42 Network](https://www.42.fr/42-network/),\n[42 São Paulo](https://www.42sp.org.br/) is a software engineering school\nthat offers a healthy alternative to traditional education:\n\n- It doesn't have any teachers and classes.\n- Students learn by cooperating\n  and correcting each other's work (peer-to-peer learning).\n- Its focus is as much on social skills as it is on technical skills.\n- It's completely free to anyone that passes its selection process -\n  [**The Piscine**](https://42.fr/en/admissions/42-piscine/)\n\nIt's an amazing school, and I'm grateful for the opportunity.\n\n## 📚 Resources \u003ca name = \"resources\"\u003e\u003c/a\u003e\n\n### `memset()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/memset.3.html\u003e\n\n### `printf()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/printf.3.html\u003e\n\n### `malloc()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/malloc.3.html\u003e\n\n### `free()`\n\n- \u003chttps://man7.org/linux/man-pages/man1/free.1.html\u003e\n\n### `write()`\n\n- \u003chttps://linux.die.net/man/2/write\u003e\n\n### `usleep()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/usleep.3.html\u003e\n\n### `gettimeofday()`\n\n- \u003chttps://man7.org/linux/man-pages/man2/gettimeofday.2.html\u003e\n- \u003chttps://stackoverflow.com/questions/60932647/gettimeofday-why-use-both-seconds-and-microseconds\u003e\n- \u003chttps://www.wake-up-neo.net/pt/c/como-obter-o-tempo-atual-em-milissegundos-partir-de-c-no-linux/970965963/\u003e\n- \u003chttps://man7.org/linux/man-pages/man2/gettimeofday.2.html\u003e\n\n### `pthread_create()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/pthread_create.3.html\u003e\n\n### `pthread_detach()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/pthread_detach.3.html\u003e\n\n### `pthread_join()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/pthread_join.3.html\u003e\n\n### `pthread_mutex_init()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/pthread_mutex_init.3p.html\u003e\n\n### `pthread_mutex_destroy()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/pthread_mutex_destroy.3p.html\u003e\n\n### `pthread_mutex_lock()`\n\n- \u003chttps://www.ibm.com/docs/en/zos/2.2.0?topic=functions-pthread-mutex-lock-wait-lock-mutex-object\u003e\n- \u003chttps://www.man7.org/linux/man-pages/man3/pthread_mutex_lock.3p.html\u003e\n\n### `pthread_mutex_unlock()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/pthread_mutex_lock.3p.html\u003e\n\n### `sem_open()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/sem_open.3.html\u003e\n\n### `sem_close()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/sem_close.3.html\u003e\n\n### `sem_post()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/sem_post.3.html\u003e\n\n### `sem_wait()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/sem_wait.3.html\u003e\n\n### `sem_unlink()`\n\n- \u003chttps://man7.org/linux/man-pages/man3/sem_unlink.3.html\u003e\n\n### `pthread.h`\n\n- \u003chttps://www.man7.org/linux/man-pages/man0/pthread.h.0p.html\u003e\n- \u003chttps://randu.org/tutorials/threads/\u003e\n- \u003chttp://files.kipr.org/gcer/2009/proceedings/Myers_ApplicationPthreads.pdf\u003e\n\n### Data Races\n\n- \u003chttps://en.cppreference.com/w/c/language/atomic\u003e\n- \u003chttps://www.delftstack.com/pt/howto/c/atomic-in-c/\u003e\n\n### Atomic Types\n\n- \u003chttps://blog.regehr.org/archives/490\u003e\n- \u003chttps://docs.oracle.com/cd/E19205-01/820-0619/geojs/index.html#:~:text=A%20data%20race%20occurs%20when,their%20accesses%20to%20that%20memory\u003e\n\n### `semaphore.h`\n\n- \u003chttps://man7.org/linux/man-pages/man7/sem_overview.7.html\u003e\n- \u003chttps://wikiless.org/wiki/Inter-process_communication?lang=en\u003e\n- \u003chttps://www.geeksforgeeks.org/use-posix-semaphores-c/\u003e\n\n### Tutorials\n\n- \u003chttps://blog.pantuza.com/artigos/o-jantar-dos-filosofos-problema-de-sincronizacao-em-sistemas-operacionais\u003e\n- \u003chttps://www.notion.so/Philosophers-2b872948598e4f0cba91c66d8b5ba821\u003e\n- \u003chttps://rodsmade.notion.site/Acelera-Philosophers-a82a52edabe24ea4a382393fae6c4531\u003e\n\n### Boards\n\n- \u003chttps://excalidraw.com/\u003e\n- \u003chttps://miro.com/app/board/o9J_l0AjIkc=/\u003e\n- \u003chttps://miro.com/app/board/uXjVOUjzO5Q=/\u003e\n\n### C Quirks\n\n- \u003chttps://www.tantalon.com/pete/cppopt/appendix.htm#AppendixB_RelativeCosts\u003e\n\n### Valgrind\n\n- \u003chttps://valgrind.org/docs/manual/drd-manual.html\u003e\n- \u003chttps://valgrind.org/docs/manual/hg-manual.html\u003e\n- \u003chttp://bl0rg.krunch.be/memleak-pthreads.html\u003e\n\n### Helgrind `Lock Order Violated` Error\n\n- \u003chttps://discord.com/channels/706206701598277681/805218621977919498/1021192139272622192\u003e\n- \u003chttps://stackoverflow.com/questions/62001623/why-does-helgrind-show-lock-order-violated-error-message\u003e\n\n### `nice`\n\n- \u003chttps://linux.die.net/man/1/nice\u003e\n\n### Visualizers\n\n- \u003chttps://nafuka11.github.io/philosophers-visualizer/\u003e\n\n### Testers\n\n- \u003chttps://github.com/GOAT095/philosophers-tester/blob/master/delay_o_meter.py\u003e\n- \u003chttps://github.com/nesvoboda/socrates\u003e\n- \u003chttps://github.com/nesvoboda/socrates/blob/master/delay_o_meter.py\u003e\n- \u003chttps://github.com/wwwwelton/philosophers/blob/master/test.sh\u003e\n- \u003chttps://pastebin.pl/view/85d80008\u003e\n\n### Videos\n\n- \u003chttps://invidious.weblibre.org/playlist?list=PLfqABt5AS4FmuQf70psXrsMLEDQXNkLq2\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=-TFqOIqP-_k\u0026ab_channel=JamieKing\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=2Jmj8F-vpfc\u0026t=109s\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=9axu8CUvOKY\u0026list=PL9IEJIKnBJjFZxuqyJ9JqVYmuFZHr7CFM\u0026index=3\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=DoYXn3nd0Ws\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=GNw3RXr-VJk\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=NbwbQQB7xNQ\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=cx1ULv4wYxM\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=d9s_d28yJq0\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=sDLQWivf1-I\u0026dark_mode=true\u003e\n- \u003chttps://invidious.weblibre.org/watch?v=trdXKhWAGdg\u0026dark_mode=true\u003e\n- \u003chttps://yewtu.be/watch?v=ukM_zzrIeXs\u003e\n\n### Python Multithreading\n\n- \u003chttps://tenthousandmeters.com/blog/python-behind-the-scenes-13-the-gil-and-its-effects-on-python-multithreading/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibrity%2Fft_philosophers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibrity%2Fft_philosophers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibrity%2Fft_philosophers/lists"}