{"id":19590653,"url":"https://github.com/aaron-22766/42_philosophers","last_synced_at":"2025-07-29T22:34:26.412Z","repository":{"id":157062347,"uuid":"633271709","full_name":"aaron-22766/42_philosophers","owner":"aaron-22766","description":"Solve the dining philosophers problem with threads and mutexes","archived":false,"fork":false,"pushed_at":"2024-02-22T12:00:33.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-09T14:54:11.734Z","etag":null,"topics":["42","42-philosophers","42born2code","42cursus","42heilbronn","42projects","42school","c","dining-philosophers","dining-philosophers-problem","mutexes","threads"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aaron-22766.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-04-27T06:31:40.000Z","updated_at":"2024-02-14T09:37:01.000Z","dependencies_parsed_at":"2024-02-22T13:25:04.166Z","dependency_job_id":"ec8a84f4-35bf-4405-ae64-ec30fab16cb4","html_url":"https://github.com/aaron-22766/42_philosophers","commit_stats":null,"previous_names":["aaron-22766/42_philosophers"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aaron-22766/42_philosophers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaron-22766%2F42_philosophers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaron-22766%2F42_philosophers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaron-22766%2F42_philosophers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaron-22766%2F42_philosophers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aaron-22766","download_url":"https://codeload.github.com/aaron-22766/42_philosophers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaron-22766%2F42_philosophers/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267772327,"owners_count":24142087,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","42-philosophers","42born2code","42cursus","42heilbronn","42projects","42school","c","dining-philosophers","dining-philosophers-problem","mutexes","threads"],"created_at":"2024-11-11T08:25:50.069Z","updated_at":"2025-07-29T22:34:26.370Z","avatar_url":"https://github.com/aaron-22766.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n\t\u003cp\u003e\n\tphilosophers\n\t\u003c/p\u003e\n\t\u003cimg src=\"https://github.com/aaron-22766/aaron-22766/blob/main/42-badges/philosopherse.png\"\u003e\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003cb\u003e\u003ci\u003eI never thought philosophy would be so deadly\u003c/i\u003e\u003c/b\u003e\u003cbr\u003e\u003cbr\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg alt=\"GitHub code size in bytes\" src=\"https://img.shields.io/github/languages/code-size/aaron-22766/42_philosophers?color=lightblue\" /\u003e\n\t\u003cimg alt=\"Code language count\" src=\"https://img.shields.io/github/languages/count/aaron-22766/42_philosophers?color=yellow\" /\u003e\n\t\u003cimg alt=\"GitHub top language\" src=\"https://img.shields.io/github/languages/top/aaron-22766/42_philosophers?color=blue\" /\u003e\n\t\u003cimg alt=\"GitHub last commit\" src=\"https://img.shields.io/github/last-commit/aaron-22766/42_philosophers?color=green\" /\u003e\n\u003c/p\u003e\n\n---\n\n## 🗣 About\n\nEat, Sleep, Spaghetti, repeat. This project is about learning how threads work by precisely timing a group of philosophers on when to pick up forks and eat spaghetti without dying from hunger. You will learn the basics of threading a process, how to create threads and work with mutexes.\n\n## 🛠 Usage\n\n```shell\n$\u003e cd philo\n$\u003e make\n$\u003e ./philo [philos] [die time] [eat time] [sleep time] [eat limit (optional)]\n```\n\n## 💬 Description\n\n### The Dining Philosophers problem\n\n* One or more philosophers sit at a round table. There is a large bowl of spaghetti in the middle of the table.\n* The philosophers alternatively eat, think, or sleep. While they are eating, they are not thinking nor sleeping; while thinking, they are not eating nor sleeping; and, of course, while sleeping, they are not eating nor thinking.\n* There are also forks on the table. There are as many forks as philosophers.\n* Because serving and eating spaghetti with only one fork is very inconvenient, a philosopher takes their right and their left forks to eat, one in each hand.\n* When a philosopher has 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.\n* Every philosopher needs to eat and should never starve.\n* Philosophers don’t speak with each other.\n* Philosophers don’t know if another philosopher is about to die.\n* No need to say that philosophers should avoid dying!\n* Your program must ot have any data races!\n\n![Dining Philosophers](https://github.com/aaron-22766/aaron-22766/blob/main/bin/images/Dining%20Philosophers.png)\n\n## ⭐️ My approach\n\n* Each fork is a mutex, which get created first\n* Philos are initialized with each having a pointer to their left ad right fork\n* They also have mutexes that protect their `eat_count` and `time_last_eaten` variables, which is important since those are monitored by the seperate monitor threads\n* Current time is saved as the start time for the simulation\n* The philo threads are created followed by the death and eat limit monitor threads\n* Each philo will run the same routine:\n\t- checks if it's only one philo, in which case he only takes the only fork and then dies\n \t- a time delay of 50µs for each odd id of the philo\n  \t- while the `exit` variable, which is set by the monitor threads, is false the following steps get repeated\n  \t- takes forks (locks mutexes), even philos from left to right, odd ones from right to left (this prevents them all taking just the left which leads to a dead lock)\n  \t- `time_last_eaten` is set to the current time\n  \t- waits the amount of `eat time`\n  \t- puts the forks back on the table (unlocks mutexes)\n  \t- `eat_count` is incremented\n  \t- waits the amount of `sleep time`\n  \t- then thinks and tries to take forks again\n * The death monitor thread just checks the `time_last_eaten` of each philo and if it's bigger than `die time` it sets the `exit` variable true\n * The eat limit monitor does the same with the `eat limit` if it was specified\n\nDuring the entire program the philos print what they are doing like this:\n```shell\n$\u003e ./philo 2 500 200 200 3\n0 2 has taken a fork\n1 2 has taken a fork\n1 2 is eating\n201 2 is sleeping\n201 1 has taken a fork\n201 1 has taken a fork\n201 1 is eating\n401 1 is sleeping\n401 2 is thinking\n401 2 has taken a fork\n401 2 has taken a fork\n401 2 is eating\n601 2 is sleeping\n601 1 is thinking\n601 1 has taken a fork\n601 1 has taken a fork\n601 1 is eating\n801 1 is sleeping\n801 2 is thinking\n801 2 has taken a fork\n801 2 has taken a fork\n801 2 is eating\n1001 1 is thinking\n1001 2 is sleeping\n1001 1 has taken a fork\n1001 1 has taken a fork\n1001 1 is eating\n1201 2 is thinking\n1201 1 is sleeping\n```\n[This really cool website](https://nafuka11.github.io/philosophers-visualizer/) lets you visualize the simulation, just copy paste the output.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaron-22766%2F42_philosophers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faaron-22766%2F42_philosophers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaron-22766%2F42_philosophers/lists"}