# oait-h-m/philosophers architecture

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

- Interactive diagram: https://gitdiagram.com/oait-h-m/philosophers
- Repository: https://github.com/oait-h-m/philosophers

## Overview

##### Project Type and Purpose
- This is a single‐process, multi‐threaded C application that simulates the Dining Philosophers synchronization problem.
- It reads command‐line arguments, initializes shared resources (forks as mutexes), spawns philosopher threads, and runs a monitor thread to detect termination conditions.

##### File Structure
- Top‐level sources:
– main.c: entry point, high‐level orchestration
– init.c: resource/module initialization (parsing args, allocating forks)
– simulation.c: philosopher thread logic (eat/think cycles)
– monitor.c: “observer” thread logic (checks for death or completion)
– pars_arguments.c: CLI parsing
– utils.c: logging, time utilities
– philo.h: shared data structures, type definitions
- Build artifact: Makefile

##### README for Insights
- Describes goal: manage N philosopher threads competing for N forks (mutexes) without deadlock/starvation.
- No external services—purely an in‐memory concurrency simulation.

##### Main Components & Their Relationships
a. Components
1) CLI / Argument Parser
2) Simulation Controller (Initialization + Resource Manager)
3) Philosopher Threads (N workers)
4) Fork Resources (array of N mutexes)
5) Monitor Thread (observer)
6) Utility Module (timing/logging)
b. Interactions
– CLI → Argument Parser: User inputs → parsed config (N, times)
– Parser → Controller: feeds configuration data
– Controller → Fork Resources: allocates & initializes mutexes
– Controller → Spawns Philosopher Threads & Monitor Thread
– Philosopher Threads ↔ Fork Resources: lock/unlock ordering
– Monitor Thread → Philosopher Threads & Controller: reads shared state to detect death/completion
– Any Thread → Utility Module: timestamped logging
c. Architectural Patterns
– Concurrency via pthreads
– “Resource Allocation” pattern: forks as shared mutexes
– Observer pattern: monitor thread watches thread states
d. Key Technologies
– C standard library
– POSIX threads (pthread_mutex_t, pthread_create, pthread_join)
– Makefile build system

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Control Flow
    CLI["CLI Interface"]:::control
    Parser["Argument Parser\n(pars_arguments.c)"]:::control
    Controller["Simulation Controller\n(main.c, init.c)"]:::control

    %% Resources
    ForkPool["Fork Pool\nmutex[N]"]:::resource
    SharedData["Shared Data Structures\n(philo.h)"]:::resource

    %% Concurrent Elements
    subgraph "Philosopher Threads" 
        direction TB
        Philosophers["Philosopher Threads\n(simulation.c)"]:::thread
    end
    Monitor["Monitor Thread\n(monitor.c)"]:::thread

    %% Utilities
    Utility["Utility Module\n(utils.c)"]:::util

    %% Build System
    Build["Build System\n(Makefile)"]:::build

    %% Connections
    CLI -->|inputs| Parser
    Parser -->|config data| Controller
    Controller -->|initialize forks| ForkPool
    Controller -->|spawns threads| Philosophers
    Controller -->|spawns observer| Monitor

    Philosophers -->|lock/unlock| ForkPool
    Monitor -->|reads state| Controller
    Monitor -->|reads state| Philosophers

    Philosophers -->|log/time calls| Utility
    Monitor -->|log/time calls| Utility
    Controller -->|log/time calls| Utility

    %% Click Events
    click Parser "https://github.com/oait-h-m/philosophers/blob/main/pars_arguments.c"
    click Controller "https://github.com/oait-h-m/philosophers/blob/main/main.c"
    click Controller "https://github.com/oait-h-m/philosophers/blob/main/init.c"
    click Philosophers "https://github.com/oait-h-m/philosophers/blob/main/simulation.c"
    click ForkPool "https://github.com/oait-h-m/philosophers/blob/main/init.c"
    click SharedData "https://github.com/oait-h-m/philosophers/blob/main/philo.h"
    click Monitor "https://github.com/oait-h-m/philosophers/blob/main/monitor.c"
    click Utility "https://github.com/oait-h-m/philosophers/blob/main/utils.c"
    click Build "https://github.com/oait-h-m/philosophers/tree/main/Makefile"

    %% Styles
    classDef control fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef thread fill:#FFA500,stroke:#333,stroke-width:1px
    classDef resource fill:#90EE90,stroke:#333,stroke-width:1px
    classDef util fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef build fill:#E0E0E0,stroke:#333,stroke-width:1px
```
