# phucnguyenlamp/btl-os-hcmut architecture

> Architecture diagram of the GitHub repository phucnguyenlamp/btl-os-hcmut, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/phucnguyenlamp/btl-os-hcmut
- Repository: https://github.com/phucnguyenlamp/btl-os-hcmut

## Overview

##### Overview and Project Type
- This project is a small-scale operating system kernel implemented in C. The purpose seems to be an educational or experimental OS (perhaps for a final project), as evidenced by modules for CPU handling, memory management (paging, virtual memory management), scheduling, and loading.
- There’s an emphasis on multi-level queue (MLQ) scheduling and paging techniques. The README’s playful tone (“GG BTL OS, mày mạnh, tao công nhận”) underscores that the project is experimental and possibly intended as a learning tool.

##### Analyzing the File Structure to Determine System Components
- Top-level Modules:
- Contains all header files defining interfaces for various modules (CPU, loader, memory, mm, scheduler, timer, queue, etc.).
- src: Houses the implementation files for critical components (cpu-tlb, cpu, loader, mem, mm, paging, queue, sched, timer). Notice modules like "mm-vm.c", "paging.c" indicate different strategies for memory management.
- input: Organized test cases/scripts for different configurations (various OS scheduling and paging implementations, process tests, scheduler tests). This shows that the project is designed to test different OS configurations.
- Makefile: Indicates an automated build process.

##### Main Components
a. OS Kernel Core:
- Central component that likely initializes and coordinates the other subsystems.
- Includes the "os.c" module which glues together functionalities.
b. CPU and TLB Modules:
- Components associated with CPU instruction execution, and TLB caching (see cpu-tlb.c, cpu-tlbcache.c).
- These modules indicate handling of low-level CPU simulation and caching for address translation.
c. Memory Management System:
- Includes modules for basic memory operations (mem, mm.c) and more sophisticated virtual memory management routines (mm-vm.c, mm-memphy.c, paging.c).
- This part handles allocation/deallocation, paging, and possibly translation lookaside buffer (TLB) operations.
d. Process Loader:
- The "loader.c/h" module is responsible for loading and initializing processes.
e. Scheduler and Queue Subsystems:
- Scheduler (sched.c/h) which likely implements multi-level queue (MLQ) scheduling (as hinted by various input test cases) and queue management (queue.c/h).
f. Timer:
- The timer module (timer.c/h) which probably drives preemptive scheduling and system ticks.
g. Testing / Input Scenarios:
- How different test cases (from the input directories) provide configurations for various scheduler and memory management setups.

##### Relationships and Interactions
- OS Kernel Core is at the heart, calling into:
- Loader to initiate process structures.
- Scheduler (and underlying queue management) to decide process execution order.
- Memory Management to allocate and manage process memory via virtual memory and paging services.
- CPU modules that simulate instruction execution along with TLB caching.
- Timer to trigger scheduling interrupts.
- The flow is likely as follows:
- Bootstrapping: The OS Kernel (os.c) initializes, invoking the loader and memory systems.
- Process Scheduling: Scheduler and queue modules manage process execution queues.
- CPU Execution: CPU modules (with TLB support) fetch and process instructions, relying on memory management for address translation.
- Timer Interrupts: Timer module issues interrupts that prompt scheduling decisions.
- External Test Inputs (from the input directory) are used to configure and validate the behavior of these modules.

##### Architectural Patterns and Design Principles
- Modular Design: Each subsystem (CPU, memory, scheduler, loader, timer) is encapsulated in its own source/header files, enabling separation of concerns.
- Layered Architecture: OS kernel core layers on top of hardware abstraction (CPU, memory) with dedicated support for scheduling and process management.
- Configurability: Use of multiple test configurations (evident in the various input directories) shows support for testing multiple algorithmic or operational strategies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Kernel["OS Kernel Core"]:::core
    CPU["CPU and TLB Modules"]:::hardware
    Memory["Memory Management System"]:::memory
    Loader["Process Loader"]:::loader
    Scheduler["Scheduler & Queue Subsystems"]:::scheduler
    Timer["Timer"]:::timer
    Tests["Testing/Input Scenarios"]:::test

    %% OS Kernel Core connections
    Kernel -->|"init_load"| Loader
    Kernel -->|"exec"| CPU
    Kernel -->|"manage"| Memory
    Kernel -->|"schedule"| Scheduler
    Kernel -->|"interrupts"| Timer
    Tests -->|"configures"| Kernel

    %% Additional interactions
    Timer -->|"trigger"| Scheduler
    CPU -->|"addr_translation"| Memory
    Memory -->|"addr_translation"| CPU

    %% Click Events for OS Kernel Core
    click Kernel "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/os.c"
    click Kernel "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/os-cfg.h"

    %% Click Events for CPU and TLB Modules
    click CPU "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/cpu.c"
    click CPU "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/cpu-tlb.c"
    click CPU "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/cpu-tlbcache.c"
    click CPU "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/cpu.h"

    %% Click Events for Memory Management System
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/mem.c"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/mm.c"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/mm-vm.c"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/mm-memphy.c"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/paging.c"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/mem.h"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/mm.h"
    click Memory "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/os-mm.h"

    %% Click Events for Process Loader
    click Loader "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/loader.c"
    click Loader "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/loader.h"

    %% Click Events for Scheduler and Queue Subsystems
    click Scheduler "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/sched.c"
    click Scheduler "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/queue.c"
    click Scheduler "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/sched.h"
    click Scheduler "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/queue.h"

    %% Click Events for Timer
    click Timer "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/src/timer.c"
    click Timer "https://github.com/phucnguyenlamp/btl-os-hcmut/blob/main/include/timer.h"

    %% Click Event for Testing/Input Scenarios
    click Tests "https://github.com/phucnguyenlamp/btl-os-hcmut/tree/main/input"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef hardware fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef memory fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef loader fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef scheduler fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef timer fill:#e2e3e5,stroke:#d6d8db,stroke-width:2px;
    classDef test fill:#fefefe,stroke:#343a40,stroke-width:2px;
```
