# sara-l-s-silva/concorrencia-so architecture

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

- Interactive diagram: https://gitdiagram.com/sara-l-s-silva/concorrencia-so
- Repository: https://github.com/sara-l-s-silva/concorrencia-so

## Overview

##### Project type and purpose
- This is a command-line, multithreaded sorting tool written in C for a university assignment.
- It reads a file of records, sorts them (by key only) using a user-specified or automatic number of threads, then writes and fsync/msync’s the output file.

##### File structure
- There is a single source file (psort14677190.c) under “Código/”.
- A PDF under “Instruções/” describes requirements.
- The README explains usage, threading requirements, and performance analysis.
- No frontend/backend split—this is a standalone CLI tool with one major binary.

##### README for insights
- Inputs: command-line arguments (<entrada>, <saída>, <threads>)
- Output: sorted file, forced to disk via fsync() or msync()
- Runtime: spawns up to N worker threads or decides N itself, performs sorting, merges results

##### Main components
a. CLI Parser
b. File I/O subsystem
- Input File Reader
- Output File Writer (with fsync/msync)
c. In-memory Record Buffer or Array
d. Thread Pool / Thread Manager
e. Worker Threads (each executes sort on a chunk)
f. Merge Module (combines sorted subarrays)
g. Synchronization Primitives (mutexes, barriers)
h. Operating System / File System (external service)

##### Relationships & data flow
- User invokes the executable → CLI Parser interprets arguments
- CLI Parser → Input File Reader loads records into memory buffer
- Thread Manager divides the buffer into chunks and spawns Worker Threads
- Worker Threads independently sort their assigned chunk (use qsort or custom)
- Upon completion, Worker Threads synchronize at a barrier → Merge Module runs in main thread
- Merge Module writes the combined sorted array → Output File Writer → OS/File System → disk
- Before exit, Output File Writer calls fsync()/msync() to flush data to disk

##### Architectural patterns & design principles
- Divide-and-conquer (split array into N parts)
- Thread-pool or dynamic thread allocation based on <threads> parameter
- -reduce style: map = sort chunks in parallel, reduce = merge sorted chunks
- Explicit synchronization to prevent race conditions (mutexes or barriers)
- Clear separation between computation (sorting) and I/O

- Clear, human-readable labels on each component
- Arrowheads that indicate the direction of data and control flow
- Callouts for critical operations (e.g., “spawn up to <threads> workers,” “fsync()”)

## Diagram (Mermaid)

```mermaid
flowchart TB
    User(("User")):::external
    User --> CLI["CLI Parser"]:::compute
    CLI --> Reader["Input File Reader"]:::io
    Reader --> Buffer["In-memory Record Buffer"]:::compute
    Buffer --> Manager["Thread Manager"]:::compute
    Manager --> Sync["Synchronization Primitives (mutexes/barriers)"]:::sync
    Sync --> Workers["Worker Threads"]:::compute
    Workers --> Merge["Merge Module"]:::compute
    Merge --> Writer["Output File Writer\n(fsync()/msync() before exit)"]:::io
    Writer --> OS["OS / File System"]:::io

    Spec["System Requirements & Protocols\n(Assignment Spec)"]:::external
    README["README.md\nUsage & Threading Guidelines"]:::external
    Spec -.-> CLI
    README -.-> CLI

    click CLI "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Reader "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Buffer "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Manager "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Sync "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Workers "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Merge "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Writer "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click OS "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Código/psort14677190.c"
    click Spec "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/Instruções/ACH2044_EP2_2024.pdf"
    click README "https://github.com/sara-l-s-silva/concorrencia-so/blob/main/README.md"

    classDef compute fill:#ccf,stroke:#036,stroke-width:1px
    classDef io fill:#cfc,stroke:#060,stroke-width:1px
    classDef sync fill:#fcd,stroke:#f80,stroke-width:1px
    classDef external fill:#eee,stroke:#999,stroke-width:1px
```
