# 2czu/philosophers architecture

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

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

## Overview

This C program runs a command-line Dining Philosophers simulation: arguments initialize shared state and fork mutexes, philosopher threads alternate actions, and a monitor stops the run on starvation or when the optional meal limit is met. The graph separates setup, thread orchestration, philosopher actions, and shared-state utilities. Relationships are limited to calls visible in the excerpts or described in the README; the header defines shared structures and interfaces.

Main entry points: [`src/simulation.c`](https://github.com/2czu/philosophers/blob/main/src/simulation.c) (Thread orchestration), [`src/main.c`](https://github.com/2czu/philosophers/blob/main/src/main.c) (Program entry), [`src/actions.c`](https://github.com/2czu/philosophers/blob/main/src/actions.c) (Philosopher actions), [`src/init.c`](https://github.com/2czu/philosophers/blob/main/src/init.c) (Argument and state setup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Setup and lifecycle"]
  node_entry["Program entry<br/>[main.c]"]
  node_initialization["Argument and state setup<br/>[init.c]"]
  node_structures["Simulation structures<br/>[philosopher.h]"]
  node_cleanup["Thread and mutex cleanup<br/>[cleanup.c]"]
end

subgraph group_runtime["Simulation runtime"]
  node_orchestration["Thread orchestration<br/>[simulation.c]"]
  node_actions["Philosopher actions<br/>[actions.c]"]
end

subgraph group_shared["Shared state and utilities"]
  node_accessors["Shared-state accessors<br/>[accessors_1.c]"]
  node_stopflag["Stop-state accessors<br/>[accessors_2.c]"]
  node_utilities["Time and status utilities<br/>[utils.c]"]
end

node_user(("CLI user"))

node_user -->|"starts"| node_entry
node_entry -->|"initializes"| node_initialization
node_initialization -->|"populates"| node_structures
node_entry -->|"starts and monitors"| node_orchestration
node_orchestration -->|"runs"| node_actions
node_actions -->|"updates and reads meal state"| node_accessors
node_orchestration -->|"checks meal state"| node_accessors
node_actions -->|"checks stop state"| node_stopflag
node_orchestration -->|"sets and checks stop state"| node_stopflag
node_actions -->|"times and prints"| node_utilities
node_orchestration -->|"checks time"| node_utilities
node_entry -->|"frees state"| node_cleanup
node_cleanup -->|"joins threads"| node_orchestration

click node_entry "https://github.com/2czu/philosophers/blob/main/src/main.c"
click node_initialization "https://github.com/2czu/philosophers/blob/main/src/init.c"
click node_structures "https://github.com/2czu/philosophers/blob/main/philosopher.h"
click node_orchestration "https://github.com/2czu/philosophers/blob/main/src/simulation.c"
click node_actions "https://github.com/2czu/philosophers/blob/main/src/actions.c"
click node_accessors "https://github.com/2czu/philosophers/blob/main/src/accessors_1.c"
click node_stopflag "https://github.com/2czu/philosophers/blob/main/src/accessors_2.c"
click node_utilities "https://github.com/2czu/philosophers/blob/main/src/utils.c"
click node_cleanup "https://github.com/2czu/philosophers/blob/main/src/cleanup.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_initialization,node_structures,node_cleanup,node_user toneBlue
class node_orchestration,node_actions toneAmber
class node_accessors,node_stopflag,node_utilities toneMint
```

## Components

### Setup and lifecycle

- [Program entry](https://github.com/2czu/philosophers/blob/main/src/main.c): `src/main.c`
- [Argument and state setup](https://github.com/2czu/philosophers/blob/main/src/init.c): `src/init.c`
- [Simulation structures](https://github.com/2czu/philosophers/blob/main/philosopher.h): `philosopher.h`
- [Thread and mutex cleanup](https://github.com/2czu/philosophers/blob/main/src/cleanup.c): `src/cleanup.c`

### Simulation runtime

- [Thread orchestration](https://github.com/2czu/philosophers/blob/main/src/simulation.c): `src/simulation.c`
- [Philosopher actions](https://github.com/2czu/philosophers/blob/main/src/actions.c): `src/actions.c`

### Shared state and utilities

- [Shared-state accessors](https://github.com/2czu/philosophers/blob/main/src/accessors_1.c): `src/accessors_1.c`
- [Stop-state accessors](https://github.com/2czu/philosophers/blob/main/src/accessors_2.c): `src/accessors_2.c`
- [Time and status utilities](https://github.com/2czu/philosophers/blob/main/src/utils.c): `src/utils.c`

### Other components

- **CLI user**

## Connections

- CLI user → Program entry: starts (evidence: `README.md`)
- Program entry → Argument and state setup: initializes (evidence: `src/main.c`)
- Argument and state setup → Simulation structures: populates (evidence: `src/init.c`)
- Program entry → Thread orchestration: starts and monitors (evidence: `src/main.c`)
- Thread orchestration → Philosopher actions: runs (evidence: `src/simulation.c`)
- Philosopher actions → Shared-state accessors: updates and reads meal state (evidence: `src/actions.c`)
- Thread orchestration → Shared-state accessors: checks meal state (evidence: `src/simulation.c`)
- Philosopher actions → Stop-state accessors: checks stop state (evidence: `src/actions.c`)
- Thread orchestration → Stop-state accessors: sets and checks stop state (evidence: `src/simulation.c`)
- Philosopher actions → Time and status utilities: times and prints (evidence: `src/actions.c`)
- Thread orchestration → Time and status utilities: checks time (evidence: `src/simulation.c`)
- Program entry → Thread and mutex cleanup: frees state (evidence: `src/main.c`)
- Thread and mutex cleanup → Thread orchestration: joins threads (evidence: `src/cleanup.c`)
