{"id":24750001,"url":"https://github.com/iaceene/philosophers_42","last_synced_at":"2026-05-05T03:33:26.953Z","repository":{"id":273403146,"uuid":"919590514","full_name":"iaceene/Philosophers_42","owner":"iaceene","description":"This project is about how to use the threads and mutexes to prevent data race, you can apply the same concepts to the dining philosophers problem.","archived":false,"fork":false,"pushed_at":"2025-02-20T17:48:08.000Z","size":38,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-20T18:41:30.023Z","etag":null,"topics":["1337school","42cursus","c","cpp","fork","philosophers","philosophers42","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/iaceene.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,"publiccode":null,"codemeta":null}},"created_at":"2025-01-20T17:09:51.000Z","updated_at":"2025-02-20T17:48:11.000Z","dependencies_parsed_at":"2025-02-11T23:39:29.694Z","dependency_job_id":null,"html_url":"https://github.com/iaceene/Philosophers_42","commit_stats":null,"previous_names":["iaceene/philosophers_42"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iaceene%2FPhilosophers_42","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iaceene%2FPhilosophers_42/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iaceene%2FPhilosophers_42/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iaceene%2FPhilosophers_42/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iaceene","download_url":"https://codeload.github.com/iaceene/Philosophers_42/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245048556,"owners_count":20552534,"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":["1337school","42cursus","c","cpp","fork","philosophers","philosophers42","threads"],"created_at":"2025-01-28T08:55:02.239Z","updated_at":"2026-05-05T03:33:26.949Z","avatar_url":"https://github.com/iaceene.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Philosophers42\n\n\n\n![iaceene](https://cs61.seas.harvard.edu/site/img/deadlock-01.png)\n\n**Philosophers** : This project is part of the **42 the network**, a unique coding school that focuses on peer-to-peer learning and real-world coding practices. The goal of the Philosophers 42 project is to develop a program that simulates philosophers thinking and eating in a shared environment, inspired by the classical synchronization problem known as the **Dining Philosophers Problem**.\n\n## Project Overview\n\nThe Dining Philosophers Problem was formulated by **Edsger Dijkstra** in 1965 and is often used to illustrate synchronization issues in concurrent programming. The problem consists of a set of philosophers sitting around a table. Each philosopher thinks and, when hungry, picks up two forks to eat. Philosophers must not starve, and they should not block each other indefinitely, ensuring a solution that avoids deadlocks and resource starvation.\n\nIn this project, you are tasked with writing a program that simulates the philosophers' behavior using threads, mutexes, and semaphores, ensuring synchronization and avoiding deadlock and resource starvation.\n\n## Objective\n\nThe main goal is to implement a solution that allows philosophers to:\n\n1. **Think**: Philosophers can think for an arbitrary amount of time.\n2. **Eat**: Philosophers can eat for an arbitrary amount of time.\n3. **Acquire forks**: Each philosopher needs two forks (mutexes) to eat.\n4. **Avoid deadlock**: Philosophers should never be in a state where they are all waiting for a fork (deadlock).\n5. **Avoid starvation**: Philosophers should not be prevented from eating for an indefinite period (starvation).\n\n## Requirements\n\n- Use **pthread** (POSIX threads) to implement concurrency in C.\n- Use **mutexes** or **semaphores** to protect shared resources.\n- The program should simulate philosophers in a circular arrangement where each philosopher can either think or eat.\n- The system should properly handle concurrency to ensure philosophers eat, think, and take turns eating without deadlocks or starvation.\n- Handle edge cases like when there are very few philosophers or when no philosophers are hungry.\n- Ensure all resources are properly cleaned up (e.g., destroying mutexes/semaphores before program termination).\n\n## How It Works\n\n### Structure of the Program:\n\n1. **Philosopher Structure**: Each philosopher is represented by a structure that includes:\n   - A unique identifier (e.g., `id`).\n   - A state (thinking, eating, waiting).\n   - Mutexes for the two forks they need to pick up and eat.\n   \n2. **Forks**: Each fork is shared between two philosophers and is represented by a mutex to avoid simultaneous access.\n\n3. **Simulation Loop**: Each philosopher operates within a loop that alternates between thinking and eating, with pauses for each action to simulate real-time behavior. Philosophers must pick up forks in a controlled manner, ensuring no deadlocks.\n\n4. **Synchronization**: Using mutexes and semaphores, philosophers must avoid blocking each other indefinitely while ensuring that only two philosophers can eat simultaneously without interference.\n\n### Key Components:\n\n- **pthread_create**: Used to create threads for each philosopher.\n- **pthread_mutex_t**: Mutexes are used to protect shared resources (the forks).\n- **pthread_join**: Used to wait for threads to finish execution before the program terminates.\n\n## How to Run\n\n1. Clone the repository:\n\n    ```bash\n    git clone https://github.com/iaceene/Philosophers_42.git\n    cd Philosophers_42\n    ```\n\n2. Compile the program:\n\n    ```bash\n    make\n    ```\n\n3. Run the program:\n\n    ```bash\n    ./philo \u003cnumber_of_philosophers\u003e \u003ctime_to_die\u003e \u003ctime_to_eat\u003e \u003ctime_to_sleep\u003e\n    ```\n\n    Replace `\u003cnumber_of_philosophers\u003e`, `\u003ctime_to_die\u003e`, `\u003ctime_to_eat\u003e`, and `\u003ctime_to_sleep\u003e` with appropriate values.\n\n    Example:\n\n    ```bash\n    ./philo 5 800 200 200\n    ```\n\n    In this example, there are 5 philosophers, each philosopher must die after 800 milliseconds of not eating, and philosophers will eat and sleep for 200 milliseconds each.\n\n## Requirements for Completion\n\n- The program must ensure that the simulation terminates after a specified amount of time or when all philosophers are able to eat.\n- The program should handle edge cases where philosophers do not get a chance to eat due to starvation.\n- Proper synchronization techniques should be used to avoid race conditions and ensure the system runs efficiently.\n\n## Resources\n\nFor additional information on synchronization and concurrency, you may want to review the following concepts:\n\n- **DEADLOCK**: [https://cs61.seas.harvard.edu/site/2018/Synch5/]\n\n- **POSIX Threads (pthreads)**: [https://man7.org/linux/man-pages/man3/pthread_create.3.html](https://man7.org/linux/man-pages/man3/pthread_create.3.html)\n- **Mutexes and Semaphores**: [https://en.wikipedia.org/wiki/Mutex](https://en.wikipedia.org/wiki/Mutex), [https://en.wikipedia.org/wiki/Semaphore_(programming)](https://en.wikipedia.org/wiki/Semaphore_(programming))\n- **Dining Philosophers Problem**: [https://en.wikipedia.org/wiki/Dining_philosophers_problem](https://en.wikipedia.org/wiki/Dining_philosophers_problem)\n\n## Contributing\n\nIf you would like to contribute to this project, feel free to submit pull requests or open issues with suggestions. All contributions are welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiaceene%2Fphilosophers_42","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiaceene%2Fphilosophers_42","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiaceene%2Fphilosophers_42/lists"}