多道程序与分时多任务
内存调度模型
flowchart LR
%% 按类型定义专属样式(遵循通俗约定:函数=蓝、结构体=绿、寄存器=紫、栈=浅红、内存=黄、管理组件=深蓝)
classDef func fill:#4299e1, stroke:#2563eb, stroke-width:2px, color:#fff, font-weight:bold, rounded:8px, font-size:14px;
classDef struct fill:#4ade80, stroke:#16a34a, stroke-width:2px, color:#1e293b, font-weight:bold, rounded:8px, font-size:14px;
classDef reg fill:#a855f7, stroke:#7e22ce, stroke-width:2px, color:#fff, font-weight:bold, rounded:6px, font-size:13px;
classDef stack fill:#fecaca, stroke:#dc2626, stroke-width:2px, color:#7f1d1d, font-weight:600, rounded:8px, font-size:13px;
classDef memory fill:#fde047, stroke:#ca8a04, stroke-width:2px, color:#78350f, font-weight:600, rounded:8px, font-size:13px;
classDef manager fill:#1e40af, stroke:#1e3a8a, stroke-width:2px, color:#fff, font-weight:bold, rounded:10px, font-size:14px;
classDef transition stroke:#64748b, stroke-width:1.5px, stroke-linecap:round, font-size:12px, font-family:Arial;
classDef subgraphTitle fill:#1e293b, font-weight:bold, font-size:14px, font-family:Arial;
%% 栈(USER_STACK/KERNEL_STACK)
subgraph USER_STACK["USER_STACK"]
direction LR
US1("..."):::stack
US0("UserStack 0"):::stack
end
subgraph KERNEL_STACK["KERNEL_STACK"]
direction LR
KS1("..."):::stack
KS0("KernelStack 0"):::stack
end
%% 内存/二进制代码
subgraph Memory["Memory"]
direction LR
M1("..."):::memory
M0("Task 0 Code"):::memory
end
%% 任务相关(结构体:TaskControlBlock/TaskStatus/TaskContext/TrapContext)
subgraph tasks["tasks"]
subgraph TaskControlBlock1["TaskControlBlock1"]
TaskContext1("..."):::struct
TaskStatus1("..."):::struct
end
subgraph TaskControlBlock0["TaskControlBlock0"]
subgraph TaskContext0["TaskContext0"]
ra0("ra"):::reg
sp0("sp"):::reg
s0("s0 ~ s11"):::reg
end
TaskStatus0("TaskStatus 0"):::struct
end
end
subgraph TrapContextList["TrapContextList"]
OtherTrapContext("..."):::struct
subgraph TrapContext["TrapContext"]
x0("x0~x31"):::reg
ssstatus0("sstatus"):::reg
sepc0("sepc"):::reg
end
end
%% 管理组件(TaskManager/TASK_MANAGER)
TaskManager("TaskManager"):::manager
TASK_MANAGER("TASK_MANAGER"):::manager
%% 函数(main/__restore)
main("main"):::func
__restore("__restore"):::func
%% 原有连接关系(保持不变,统一应用transition样式)
TaskManager -->|延迟加载全局唯一实例| TASK_MANAGER:::transition
main -->|1. load_apps 加载应用代码| Memory:::transition
main -->|2. run_first_task触发初始化| TASK_MANAGER:::transition
TASK_MANAGER -.->|初始化| tasks:::transition
sp0 -.->|__restore使用sp作为参数恢复上下文| __restore:::transition
sepc0 -.-> M0:::transition
x0 -.->|sp| US0:::transition
KS0 -.->|存储了TrapContext| TrapContext:::transition
ra0 --> __restore:::transition
sp0 --> KS0:::transition
%% 优化子图标题样式(统一居中、加粗)
style USER_STACK fill:#fef2f2, stroke:#fecaca, stroke-width:1px, padding:10px;
style KERNEL_STACK fill:#fef2f2, stroke:#fecaca, stroke-width:1px, padding:10px;
style Memory fill:#fffbeb, stroke:#fde047, stroke-width:1px, padding:10px;
style tasks fill:#ecfccb, stroke:#4ade80, stroke-width:1px, padding:10px;
style TrapContextList fill:#ecfccb, stroke:#4ade80, stroke-width:1px, padding:10px;
实现
应用加载