# wormav/philosophers architecture

> Architecture diagram of the GitHub repository wormav/philosophers, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wormav/philosophers
- Repository: https://github.com/wormav/philosophers

## Overview

The project “Philosophers” is a C-based simulation of the classical dining philosophers problem. In this project, you simulate multiple philosopher threads competing for limited forks (resources), where each philosopher follows a cycle (thinking, eating, and sleeping) under strict timing constraints. The simulation ends when either a philosopher dies (failing to eat in time) or, in one variant, when every philosopher has eaten a prescribed number of times.

##### Main Components
- Input/Parsing Module: Represented by files in philo/src/parsing, this module handles validating and processing command line arguments (number_of_philosophers, time_to_die, etc.).
- Memory & Thread Management Module: Found in philo/src/memory, these files (free.c, threads.c) manage memory allocation and the creation/joining of philosopher threads.
- Philosopher Lifecycle Module: Under philo/src/philos, this module encapsulates the behavior of each philosopher. It contains routines for life-cycle management (routine.c, routine_utils.c), printing actions (print_action.c), and handling the single philosopher case (single_philo.c).
- Simulation Initialization Module: In philo/src/sim, this module (sim_init.c) configures and starts the simulation, setting initial conditions for the philosophers.
- Utility Module: Located in philo/src/utils, it contains helper functions for time management and other general-purpose routines.
- Main Application Entry Point: The main.c in philo/src ties all the modules together, initializing the simulation, dispatching threads, and managing overall execution.

##### Relationships and Interactions
- The Main Entry Point (“main.c”) initiates the simulation by first invoking functions in the Parsing Module to process inputs.
- Once input is validated, control passes to the Simulation Initialization Module to set up shared resources (like forks) and data structures.
- The Memory Management and Threads Module is then engaged to allocate necessary structures and to spawn concurrent philosopher threads.
- Each philosopher thread then operates under the control of the Philosopher Lifecycle Module, which calls into Utility functions (time handling, logging actions) and manages their state transitions.
- Data flows upward: the simulation’s state (e.g., timeouts, eating cycles) is monitored and printed using functions from print_action.c.
- Clean-up routines provided in the Memory Module ensure that resources are freed once the simulation ends.

##### Architectural Patterns and Design Principles
- Modularity: The project is neatly divided into modules (parsing, memory, simulation, lifecycle, utilities) that encapsulate specific responsibilities.
- Concurrency: The design inherently leverages multithreading principles, with each philosopher operating on its own thread.
- Separation of Concerns: Input processing, simulation setup, thread management, philosopher dynamics, and utility functions are decoupled from one another.
- Resource Management: As evidenced by the dedicated memory management files, careful management of resources (allocation/freeing) is a design consideration.

##### Components
- A block labeled “Input Parsing” representing philo/src/parsing.
- A block for “Simulation Initialization” showing philo/src/sim.
- A block for “Philosopher Lifecycle/Management” representing philo/src/philos.
- A block for “Memory & Thread Management” representing philo/src/memory.
- A block for “Utilities” representing philo/src/utils.
- A block for “Main” indicating the application entry point.
­ – “Main” calls “Parsing”.
­ – “Main” then calls “Simulation Initialization”.
­ – “Simulation Initialization” requests resources from “Memory & Thread Management”.
­ – “Memory/Thread Management” spawns multiple threads for the “Philosopher Lifecycle” module.
­ – “Philosopher Lifecycle” interacts with “Utilities” (for actions like printing and time tracking).
- Optionally, annotate where concurrency (threads) and synchronization (shared resources like forks) occur.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Bootstrap"
        MAIN["Main (Entry Point)"]:::control
    end

    subgraph "Setup"
        PARS["Input Parsing"]:::control
        SIM["Simulation Initialization"]:::control
    end

    subgraph "Management"
        MEM["Memory & Thread Management"]:::concurrency
    end

    subgraph "Philosopher Operations"
        PHILO["Philosopher Lifecycle"]:::concurrency
        UTIL["Utilities"]:::utility
    end

    MAIN -->|"calls"| PARS
    MAIN -->|"calls"| SIM
    SIM -->|"requests_resources"| MEM
    MEM -->|"spawns_threads_for"| PHILO
    PHILO -->|"uses"| UTIL

    click MAIN "https://github.com/wormav/philosophers/blob/main/philo/src/main.c"
    click PARS "https://github.com/wormav/philosophers/tree/main/philo/src/parsing"
    click SIM "https://github.com/wormav/philosophers/blob/main/philo/src/sim/sim_init.c"
    click MEM "https://github.com/wormav/philosophers/tree/main/philo/src/memory"
    click PHILO "https://github.com/wormav/philosophers/tree/main/philo/src/philos"
    click UTIL "https://github.com/wormav/philosophers/tree/main/philo/src/utils"

    classDef control fill:#ffdead,stroke:#8b4513,stroke-width:2px;
    classDef concurrency fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef utility fill:#90ee90,stroke:#006400,stroke-width:2px;
```
