{"id":23307057,"url":"https://github.com/whoismtrx/42_philosophers","last_synced_at":"2025-04-07T00:30:05.736Z","repository":{"id":231641189,"uuid":"503559956","full_name":"whoismtrx/42_Philosophers","owner":"whoismtrx","description":"In the 42 philosophers project, the goal is to develop a synchronization mechanism that enables a group of philosophers to share limited resources while avoiding issues like deadlock.","archived":false,"fork":false,"pushed_at":"2024-04-05T03:01:08.000Z","size":175,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T06:17:09.869Z","etag":null,"topics":["1337","1337cursus","1337school","42","42cursus","42projects","42school","c","clang","clanguage","philosophers","philosophers-42","philosophers-dinner-problem","philosophers42","processes","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/whoismtrx.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":"2022-06-15T00:17:09.000Z","updated_at":"2024-04-14T13:01:06.000Z","dependencies_parsed_at":"2024-04-05T04:20:52.178Z","dependency_job_id":"9943de53-98f9-40f9-a311-417976e1a5aa","html_url":"https://github.com/whoismtrx/42_Philosophers","commit_stats":null,"previous_names":["whoismtrx/42_philosophers"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoismtrx%2F42_Philosophers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoismtrx%2F42_Philosophers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoismtrx%2F42_Philosophers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoismtrx%2F42_Philosophers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whoismtrx","download_url":"https://codeload.github.com/whoismtrx/42_Philosophers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247574089,"owners_count":20960495,"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","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":["1337","1337cursus","1337school","42","42cursus","42projects","42school","c","clang","clanguage","philosophers","philosophers-42","philosophers-dinner-problem","philosophers42","processes","threads"],"created_at":"2024-12-20T12:20:47.011Z","updated_at":"2025-04-07T00:30:05.681Z","avatar_url":"https://github.com/whoismtrx.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Philosophers\n\n![](https://github.com/whoismtrx/42_Philosophers/blob/main/philosophers.gif)\n\n## Overview\n\nThis project is a simulation of the dining philosophers problem. The dining philosophers problem is a classic synchronization problem that models a situation where some philosophers are sitting at a table and they spend their time thinking and eating and sleeping. The philosophers share a common resources, table and forks and have to use them in a way that they don't starve and don't create a deadlock.\n\n## Key Features\n\nThe project is implemented in `C` and uses `pthread` library for `thread` management in `Mandatory` part and `fork` for `process` management in `Bonus` part. The project has the following key features:\n- Each philosopher is a separate thread/process depending on the part.\n- Each philosopher has a `fork` on the left and a `fork` on the right.\n- A philosopher can only eat if he has both `forks`.\n- A fork can be `Mutex` or `Semaphore` depending on the part.\n- Each philosopher has a state: `thinking`, `eating`, `sleeping`, `dead`.\n- Each philosopher has a `time_to_die`, `time_to_eat`, `time_to_sleep` and `number_of_times_to_eat`.\n- The simulation stops when a philosopher dies or when all philosophers have eaten `number_of_times_to_eat` times.\n\n## Getting Started\n\nTo get a local copy of the project, perform the following steps:\n```\ngit clone https://github.com/whoismtrx/42_Philosophers.git philosophers\ncd philosophers\n```\nfor Mandatory part:\n```\ncd philo\nmake\n```\nfor Bonus part:\n```\ncd philo_bonus\nmake\n```\n\n## Usage\n\nTo run the simulation, execute the following command:\n```\n./philo 4 410 200 200 [5]\n```\nwhere:\n- `4` is the number of philosophers.\n- `410` is the time to die in milliseconds.\n- `200` is the time to eat in milliseconds.\n- `200` is the time to sleep in milliseconds.\n- `[5]` is the number of times each philosopher must eat. If not provided, the simulation stops when a philosopher dies.\n\n## Implementation\n\nPhilosophers is a project that requires a good understanding of `synchronization` and `parallel computing`. first we need to understand the `dining philosophers` problem and then implement a solution that satisfies the requirements of the project.\nWe need to create a `simulation` where `philosophers` are `threads` so basically we need to create a separate `thread` for each `philosopher` with its own `routine`. Each philosopher has a `state` and a `set of actions` that he can perform. The philosophers share a common `resources`, the `table` and `forks`, the table its the `process` itself and the forks are the `mutexes` that we used to `lock` and `unlock` the forks, so let's start philosopher routine. `Routing` is simple, philosopher will `think`, then try to take his `left` and `right` forks, if he can't take both of them he stay in the same state and try again, when he take both forks he will `eat`, then he will release the forks and `sleep` and then he will `think` again and so on. The simulation stops when a philosopher `dies` or when all philosophers have eaten `number_of_times_to_eat` times. you can create a thread who `monitors` the philosophers and check if they are alive and if they have eaten enough times.\nThe `bonus` part is the same as the `mandatory` part but instead of `threads` we use `processes`, so we need to create a separate process for each philosopher with its own routine. and because processes `don't share memory` we need to use `shared memory` to store the state of the philosophers and the forks. we can use `semaphores` to lock and unlock the forks.\nthe rest of the implementation is the same as the mandatory part.\n\n## Resources\n\n- [Dining Philosophers Problem](https://lass.cs.umass.edu/~shenoy/courses/fall13/lectures/Lec10_notes.pdf)\n- [UNIX Threads in C](https://www.youtube.com/watch?v=d9s_d28yJq0\u0026list=PLfqABt5AS4FmuQf70psXrsMLEDQXNkLq2)\n- [UNIX Processes in C](https://www.youtube.com/watch?v=cex9XrZCU14\u0026list=PLfqABt5AS4FkW5mOn2Tn9ZZLLDwA3kZUY)\n- For a good explanation, you can check [zelhajou](https://github.com/zelhajou) repository for the [philosophers](https://github.com/zelhajou/42-philosophers) project.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhoismtrx%2F42_philosophers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhoismtrx%2F42_philosophers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhoismtrx%2F42_philosophers/lists"}