zneel / philosophers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


philosophers

โ—ฆ Connecting minds. Unleashing ideas.

โ—ฆ Developed with the software and tools listed below.

C

GitHub top language GitHub code size in bytes GitHub commit activity GitHub license

๐Ÿ“’ Table of Contents


๐Ÿ“ Overview

This project is a program that simulates the "dining philosophers" problem. It allows for the control of philosopher behavior and their interactions with forks, as well as time management. The code provides functionalities such as initialization of the simulation, allocating memory for philosophers and forks, executing the simulation, and checking for cases like deadlocks or if philosophers have eaten enough. The project's value proposition lies in its ability to provide a realistic simulation of the dining philosophers problem and evaluate different strategies to address it.


โš™๏ธ Features

Feature Description
โš™๏ธ Architecture The codebase follows a multithreaded design using the POSIX threads (pthreads) library. Each philosopher is represented as a thread, and the interactions and synchronization between philosophers are accomplished using mutex locks for fork access.
๐Ÿ“– Documentation The codebase is well-documented with detailed explanations of each file, function, and data structure. The documentation clarifies the purpose and functionality of different components.
๐Ÿ”— Dependencies The codebase relies on the external library "libft" for additional functionalities. This library is not included in the repository and needs to be separately obtained and compiled.
๐Ÿงฉ Modularity The codebase is organized into separate files that define specific components of the program, such as philosophers, forks, time management, utilities, actions, and the main module. Each file has a clear purpose and consolidates related functions and data structures.
โœ”๏ธ Testing The codebase lacks explicit testing mechanisms. However, the structure of the code, the separation of concerns, and the usage of mutex locks indicate a potential for unit testing of individual components.
โšก๏ธ Performance The emphasis of the codebase is on simulation rather than performance optimization. Resource usage may not be highly optimized due to the workload of managing multiple threads.
๐Ÿ” Security The codebase does not deal with sensitive data, and there are no specific security measures implemented. However, careful use of mutex locks ensures thread safety and prevents race conditions.
๐Ÿ”€ Version Control The codebase is version controlled using Git. The repository provides a detailed history of commits, allowing for easy tracking of changes and collaboration among developers.
๐Ÿ”Œ Integrations The codebase does not include explicit integrations with other systems or services. However, it can be integrated into larger applications as a module for simulating the dining philosopher problem.
๐Ÿ“ถ Scalability The codebase is designed to handle scalability by representing each philosopher as a separate thread. Adding or removing philosophers can be easily achieved by extending or reducing the size of data structures and launching or terminating threads accordingly.

๐Ÿ“‚ Project Structure


๐Ÿงฉ Modules

Philo
File Summary
Makefile This code is a program that simulates the "dining philosophers" problem. It includes functionalities to control the behavior of the philosophers and their interactions with forks, as well as time management and various utility functions. The code can be compiled with additional flags for debugging and sanitizing. It depends on a separate library called "libft.
Src
File Summary
philosophers.c The code creates a simulation of philosophers dining at a table. It initializes and assigns properties to philosophers, allocates memory, starts threads for each philosopher's routine, and checks for cases such as deadlocks or if they have eaten enough.
time.c This code provides functionality for measuring, calculating, and managing time-related operations. It includes functions to convert time to milliseconds, get the current time, calculate the time difference, and induce a delay in milliseconds.
helpers.c The code includes a function called print_usage() that prints out a specific usage message for a program called "./philo". It takes in five arguments and displays them as part of the message.
checker.c The "checker.c" code contains the core functionalities of the checker thread in a philosopher simulation program. It includes functions to check if a philosopher is dead, if a particular philosopher has reached the desired number of meals, and if all philosophers have reached the desired number of meals. It also includes a function to periodically check for dead philosophers and end the simulation if necessary. The code is multi-threaded and uses mutex locks to ensure thread safety.
utils.c The code in utils.c includes utility functions for the "philo" program.-ft_swap(): Swaps two integers.-print_dead(): Prints the time of death for a philosopher.-sim_print(): Prints the current action of a philosopher if the simulation is not over.-sim_end(): Checks if the simulation has ended.
actions.c This code defines three core actions for the philosopher simulation. The p_eat() function handles the logic for a philosopher to eat by acquiring two forks, updating their stats, and releasing the forks. The p_think() function prints a message indicating the philosopher is thinking. The p_sleep() function prints a message indicating the philosopher is sleeping for a specified duration.
main.c This code parses command line arguments, initializes a simulation, and runs it based on the given parameters. It also performs basic validation of the arguments and implements error handling.
simulation.c The code is for a simulation of a dining philosopher problem. It initializes the simulation, allocates memory for philosophers and forks, initializes forks and philosophers, executes the simulation, and finally, destroys the simulation. It also waits for all the threads to finish before destroying the simulation.
routine.c The code is part of a simulation program for a dining philosophers problem. It includes functions to check if a philosopher has eaten enough, check if the simulation should stop, and the routine function for each philosopher's thread. The routine function simulates the actions of a philosopher, such as eating, sleeping, and thinking, based on certain conditions.
forks.c The code initializes and allocates mutex locks for forks in a dining philosophers simulation. It also includes functions to initialize and destroy the mutex locks.
Include
File Summary
philo.h The code consists of a simulation of a dining philosophers problem with multiple functions and structs. It handles the allocation and initialization of philosophers and forks, simulation control, printing, time calculations, and the main thread for checking if philosophers have finished eating their required number of times.

๐Ÿš€ Getting Started

โœ”๏ธ Prerequisites

Before you begin, ensure that you have the following prerequisites installed:

  • โ„น๏ธ Requirement 1
  • โ„น๏ธ Requirement 2
  • โ„น๏ธ ...

๐Ÿ“ฆ Installation

  1. Clone the philosophers repository:
git clone https://github.com/zneel/philosophers
  1. Change to the project directory:
cd philosophers
  1. Install the dependencies:
gcc -o myapp main.c

๐ŸŽฎ Using philosophers

./myapp

๐Ÿงช Running Tests

/* No common unit test framework in C */

๐Ÿ—บ Roadmap

  • โ„น๏ธ Task 1: Implement X
  • โ„น๏ธ Task 2: Refactor Y
  • โ„น๏ธ ...

๐Ÿค Contributing

Contributions are always welcome! Please follow these steps:

  1. Fork the project repository. This creates a copy of the project on your account that you can modify without affecting the original project.
  2. Clone the forked repository to your local machine using a Git client like Git or GitHub Desktop.
  3. Create a new branch with a descriptive name (e.g., new-feature-branch or bugfix-issue-123).
git checkout -b new-feature-branch
  1. Make changes to the project's codebase.
  2. Commit your changes to your local branch with a clear commit message that explains the changes you've made.
git commit -m 'Implemented new feature.'
  1. Push your changes to your forked repository on GitHub using the following command
git push origin new-feature-branch
  1. Create a new pull request to the original project repository. In the pull request, describe the changes you've made and why they're necessary. The project maintainers will review your changes and provide feedback or merge them into the main branch.

๐Ÿ“„ License

This project is licensed under the โ„น๏ธ INSERT-LICENSE-TYPE License. See the LICENSE file for additional info.


๐Ÿ‘ Acknowledgments

  • โ„น๏ธ List any resources, contributors, inspiration, etc.

About


Languages

Language:C 96.5%Language:Makefile 3.5%