# zelhajou/ft_unix_philosophers architecture

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

- Interactive diagram: https://gitdiagram.com/zelhajou/ft_unix_philosophers
- Repository: https://github.com/zelhajou/ft_unix_philosophers

## Overview

##### Main Components

a) Core Components:
- Philosopher Threads
- Fork Mutexes
- Health Monitor
- Main Controller
- Status Logger

b) Key Interactions:
- Thread Management
- Resource Allocation
- State Monitoring
- Synchronization Mechanisms

**┌─────────────────────────────────────────────┐**
- Main Controller              │
- (simulation_init.c, main.c)               │
- ┤

- ┌─────────┐  ┌─────────┐  ┌─────────┐   │
- Philo 1 │  │ Philo 2 │  │ Philo n │   │
- ┘  └─────────┘  └─────────┘   │

- ┌─────────┐  ┌─────────┐  ┌─────────┐   │
- Fork 1  │  │ Fork 2  │  │ Fork n  │   │
- ┘  └─────────┘  └─────────┘   │

- ┤
- Health Monitor System             │
- (health_monitor.c)                │
- ┤
- Status Logger                  │
- (print_status.c)                  │
- ┘

##### Component Details

a) Philosopher Thread (from philo_threads.c):
- State transitions (thinking, eating, sleeping)
- Mutex interaction points
- Timing constraints

b) Fork Management:
- Mutex locking/unlocking
- Resource sharing between adjacent philosophers
- Fork availability status

c) Health Monitor:
- Death detection system
- Timing verification
- State monitoring

d) Main Controller:
- Thread creation and management
- Initial setup and configuration
- Simulation parameters

##### Additional Elements

Include:
- Critical section indicators
- Timing constraints
- Resource allocation rules
- State transition conditions

##### Implementation Details

Functions from source files:
- simulation_init.c: Initialization logic
- philo_actions.c: Philosopher behaviors
- philo_states.c: State management
- health_monitor.c: Death detection
- utils/*.c: Support functions

##### Synchronization Points

Highlight:
- Mutex lock/unlock operations
- Thread creation/joining points
- Death check synchronization
- Status update mechanisms

This system design will help developers understand both the high-level architecture and the specific implementation details of the dining philosophers solution.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Main
        MC["Main Controller"]
        SI["Simulation Initializer"]
    end

    subgraph Philosophers
        PT["Thread Manager"]
        PA["Actions Handler"]
        PS["State Manager"]
    end

    subgraph Resources
        RM["Resource Manager"]
        F1["Fork 1"]
        F2["Fork 2"]
        F3["Fork n"]
    end

    subgraph Monitoring
        HM["Health Monitor"]
        SL["Status Logger"]
    end

    subgraph Utils
        PA["Argument Parser"]
        VA["Argument Validator"]
        UT["Utilities"]
        LF["LibFT"]
    end

    %% Main Controller relationships
    MC --> SI
    SI --> PT
    SI --> RM

    %% Philosopher relationships
    PT --> PA
    PT --> PS
    PA --> RM
    PS --> SL

    %% Resource relationships
    RM --> F1
    RM --> F2
    RM --> F3

    %% Monitoring relationships
    HM --> PT
    HM --> SL
    SL --> PT

    %% Utility relationships
    PA --> MC
    VA --> MC
    UT --> MC
    LF --> MC

    %% Click events for component mapping
    click MC "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/src/main.c"
    click SI "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/src/simulation_init.c"
    click PT "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/src/philo_threads.c"
    click PA "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/src/philo_actions.c"
    click PS "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/src/philo_states.c"
    click HM "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/src/health_monitor.c"
    click SL "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/utils/print_status.c"
    click PA "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/utils/parse_args.c"
    click VA "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/utils/validate_args.c"
    click UT "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/utils/utils.c"
    click LF "https://github.com/zelhajou/ft_unix_philosophers/blob/main/philo/utils/libft.c"

    %% Styling
    classDef main fill:#FFE5CC,stroke:#FF9933
    classDef philo fill:#CCE5FF,stroke:#3399FF
    classDef resource fill:#FFB3B3,stroke:#FF6666
    classDef monitor fill:#CCFFCC,stroke:#66CC66
    classDef utils fill:#E5CCFF,stroke:#9933FF

    class MC,SI main
    class PT,PA,PS philo
    class RM,F1,F2,F3 resource
    class HM,SL monitor
    class PA,VA,UT,LF utils
```
