# mcombeau/philosophers architecture

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

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

## Overview

##### Main components
- Input/Configuration: Represents the command-line argument parsing that sets up simulation variables such as the number of philosophers, time constraints, and optionally, how many times each philosopher must eat.
- Simulator/Initializer Module: The initialization routines (e.g., init.c) are responsible for setting up the simulation, creating philosopher entities, and initializing forks (mutexes for the thread version; semaphores for the processes version).
- Philosopher Entity: Each philosopher is represented by a thread (in "philo") or a process (in "philo_bonus"). These components are responsible for executing the philosopher routines (eat, sleep, think) and handling shared resources.
- Forks/Resources Manager: In the mandatory part, each fork is modeled as a mutex residing between philosophers. In the bonus part, a counting semaphore represents the pool of available forks.
- Monitor: The "grim_reaper.c" file indicates a monitoring module that checks whether a philosopher has exceeded his time limit and has died.
- Output Module: The output routines (such as output.c) are used to print timestamped actions of each philosopher including taking a fork, eating, sleeping, thinking, and dying.

##### Relationships and interactions
- The Input/Configuration component feeds parameters into the Initializer.
- The Initializer creates and spawns multiple Philosopher Entities, each operating concurrently using either threads or separate processes.
- Each Philosopher Entity interacts with the Forks/Resources Manager to acquire or release forks when eating.
- The Monitor component continuously observes the state of each philosopher, signaling if a philosopher dies (triggering the corresponding exit routines).
- The Output Module logs various state changes across the system, showing real-time activity based on scheduled operations.

##### Architectural patterns and design principles
- Concurrency Management – Use of threads and mutexes in one implementation versus processes and semaphores in the other.
- Modularity – Separation of concerns is clear in the source files. Each file (init.c, philosopher.c, managing time, cleanup, etc.) corresponds to a specific role in the simulation.
- Resource Synchronization – The system employs different synchronization primitives depending on the implementation. (A clear depiction of mutex-protected forks versus semaphore-protected fork pools will be important.)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Thread-based Implementation (philo)
    subgraph "Thread-based Implementation (philo)"
        P1["Command Line Parser"]:::thread
        P2["init.c - Initialization"]:::thread
        P3["philosopher.c - Philosopher Entity"]:::thread
        P4["Mutex Fork Manager"]:::threadResource
        P5["grim_reaper.c - Monitor"]:::thread
        P6["output.c - Output Module"]:::thread
        P1 -->|"feeds"| P2
        P2 -->|"creates"| P3
        P2 -->|"sets up"| P4
        P3 -->|"uses"| P4
        P3 -->|"reports"| P5
        P3 -->|"logs"| P6
        P5 -->|"triggers"| P6
    end

    %% Process-based Implementation (philo_bonus)
    subgraph "Process-based Implementation (philo_bonus)"
        B1["Command Line Parser"]:::process
        B2["init.c - Initialization"]:::process
        B3["philosopher.c - Philosopher Entity"]:::process
        B4["Semaphore Manager"]:::processResource
        B5["grim_reaper.c - Monitor"]:::process
        B6["output.c - Output Module"]:::process
        B1 -->|"feeds"| B2
        B2 -->|"creates"| B3
        B2 -->|"sets up"| B4
        B3 -->|"uses"| B4
        B3 -->|"reports"| B5
        B3 -->|"logs"| B6
        B5 -->|"triggers"| B6
    end

    %% Click Events for Mandatory Components
    click P1 "https://github.com/mcombeau/philosophers/blob/main/philo/sources/parsing.c"
    click P2 "https://github.com/mcombeau/philosophers/blob/main/philo/sources/init.c"
    click P3 "https://github.com/mcombeau/philosophers/blob/main/philo/sources/philosopher.c"
    click P5 "https://github.com/mcombeau/philosophers/blob/main/philo/sources/grim_reaper.c"
    click P6 "https://github.com/mcombeau/philosophers/blob/main/philo/sources/output.c"

    %% Click Events for Bonus Components
    click B1 "https://github.com/mcombeau/philosophers/blob/main/philo_bonus/sources/parsing.c"
    click B2 "https://github.com/mcombeau/philosophers/blob/main/philo_bonus/sources/init.c"
    click B3 "https://github.com/mcombeau/philosophers/blob/main/philo_bonus/sources/philosopher.c"
    click B4 "https://github.com/mcombeau/philosophers/blob/main/philo_bonus/sources/ipc.c"
    click B5 "https://github.com/mcombeau/philosophers/blob/main/philo_bonus/sources/grim_reaper.c"
    click B6 "https://github.com/mcombeau/philosophers/blob/main/philo_bonus/sources/output.c"

    %% Styles
    classDef thread fill:#f6d55c,stroke:#000,stroke-width:2px;
    classDef threadResource fill:#f9c74f,stroke:#000,stroke-width:2px;
    classDef process fill:#3caea3,stroke:#000,stroke-width:2px;
    classDef processResource fill:#20639b,stroke:#000,stroke-width:2px;
```
