# whitespaghet/panaderia architecture

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

- Interactive diagram: https://gitdiagram.com/whitespaghet/panaderia
- Repository: https://github.com/whitespaghet/panaderia

## Overview

##### Project Type & Purpose
- Panader-a is a standalone Python simulation of Lamport’s bakery algorithm for mutual exclusion.
- It uses the `multiprocessing` library to create concurrent processes that compete for a critical section, with ASCII‐art console output showing each process’s state.

##### Key Architectural Components
a. Master (Orchestrator) Process
- Initializes shared data structures (flags, tickets) via a `multiprocessing.Manager` or shared `Array`/`Value`.
- Spawns N worker processes.
b. Shared State Store
- A manager‐backed dictionary or shared arrays holding:
- choosing[i] flags
- ticket[i] numbers
c. Worker Processes (P0…Pn)
- Each executes the bakery protocol loop:
1. Set choosing[i] = True
2. Assign ticket[i] = 1 + max(ticket[0..n])
3. Set choosing[i] = False
4. Wait until for all j: (!choosing[j] && (ticket[j] == 0 || (ticket[i], i) < (ticket[j], j)))
5. Enter critical section & print ASCII state
6. Set ticket[i] = 0
7. Repeat
d. Critical Section (CS)
e. Console / ASCII Renderer
- Captures & displays process states; optional separate component or simply part of each worker’s output.

##### Relationships & Data Flows
- Master → Shared State: “initialize”
- Master → Worker Pi: “spawn”
- Worker Pi ↔ Shared State: “read/write choosing[] & ticket[]”
- Worker Pi → Critical Section: “enter when conditions met”
- Worker Pi → Console: “print ASCII status”
- Worker Pi → Shared State: “ticket[i] = 0 on exit”

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Components
    Master["Master Process"]:::master
    Shared["Shared State Store\n(choosing[], ticket[])"]:::shared
    subgraph "Worker Processes (P0..Pn)"
        Worker["Worker Pi"]:::worker
    end
    CS["Critical Section"]:::cs
    Console["Console / ASCII Renderer"]:::console
    Doc["Project Documentation"]:::doc

    %% Relationships & Data Flows
    Master -->|"initialize"| Shared
    Master -->|"spawn"| Worker
    Worker -.->|"read/write choosing[] & ticket[]"| Shared
    Worker -->|"enter when conditions met"| CS
    Worker -->|"print ASCII status"| Console
    Worker -.->|"ticket[i]=0 on exit"| Shared

    %% Click Events
    click Master "https://github.com/whitespaghet/panaderia/blob/main/code.py"
    click Shared "https://github.com/whitespaghet/panaderia/blob/main/code.py"
    click Worker "https://github.com/whitespaghet/panaderia/blob/main/code.py"
    click CS "https://github.com/whitespaghet/panaderia/blob/main/code.py"
    click Console "https://github.com/whitespaghet/panaderia/blob/main/code.py"
    click Doc "https://github.com/whitespaghet/panaderia/blob/main/README.md"

    %% Styles
    classDef master fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef shared fill:#FFA500,stroke:#000,stroke-width:1px
    classDef worker fill:#90EE90,stroke:#000,stroke-width:1px
    classDef cs fill:#F08080,stroke:#000,stroke-width:1px
    classDef console fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef doc fill:#FFFACD,stroke:#000,stroke-width:1px
```
