bupt_os_lab1
1.2024年7.4-7.8学习总结/暑假day7-112.2024.6.6学习java&算法3.2024年6.7--6.17学习总结4.2024年7.3学习总结/暑假day65.2024年6.27-7.2学习总结/暑假day1--56.2024年6.23-6.26学习总结7.2024年6.18-6.22学习总结8.2024年7.9-7.19学习总结/暑假day12-229.2024年7.26-7.29学习总结/day29-3210.编程日记 批量导入数据11.编程日记 后端使用redis12.编程日记 更改redis存储默认序列化器13.ide启动多个实例14.session和cookie15.java多线程
16.bupt_os_lab1
17.11.12 ali-oss上传图片18.sql 166119.bupt_os_lab220.11.18 学习21.11.21 打工22.IntelliJ IDEA格式化快捷键失效23.leetcode78 子集24.leetcode39 组合总和25.11.30学习日记26.12.6详解前后端对接27.2024.12.9 小bug28.12.23软工踩坑29.leetcode 104530.2024.12.26 os lab331.2024.12.27复习日记32.一文搞定宝塔LINUX部署上线前后端分离项目33.2025.1.2复习34.leetcode 178935.leetcode131 分割回文串36.2025.1.13 redis乱码问题解决37.2025.1.15 学习38.2025.2.17 学习39.2025.2.18 学习40.2025.2.19 学习41.2025.2.24学习42.git clone问题解决43.2025.2.27 学习44.2025.3.4 学习bupt_os lab1作业
1.编写代码
uthread.h
#ifndef UTHREAD_H
#define UTHREAD_H
#define STACK_SIZE 4096
enum thread_state {
THREAD_INIT,
THREAD_RUNNING,
THREAD_STOP,
THREAD_SUSPENDED,
};
struct context {
long long rip, rsp, rbp, rbx, r12, r13, r14, r15;
long long rdi, rsi, rdx;
};
struct uthread {
char stack[STACK_SIZE];
struct context context;
enum thread_state state;
const char *name;
};
void init_uthreads();
void schedule();
struct uthread *uthread_create(void (*func)(void *), void *arg, const char* thread_name);
void uthread_resume(struct uthread *tcb);
long long uthread_yield();
void thread_destroy(struct uthread *tcb);
#endif
uthread.c
#include "uthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#define STACK_SIZE 4096
static struct uthread *current_thread = NULL;
static struct uthread *main_thread = NULL;
static struct uthread *thread_queue[10];
static int thread_count = 0;
static int current_index = 0;
jmp_buf main_context;
void thread_switch(struct context *from, struct context *to);
void _uthread_entry(struct uthread *tcb, void (*thread_func)(void *), void *arg) {
tcb->state = THREAD_RUNNING;
thread_func(arg);
tcb->state = THREAD_STOP;
longjmp(main_context, 1); // 切换回主线程
}
struct uthread *uthread_create(void (*func)(void *), void *arg, const char* thread_name) {
struct uthread *uthread = malloc(sizeof(struct uthread));
if (!uthread) {
printf("Error allocating memory for thread\n");
exit(1);
}
memset(uthread, 0, sizeof(struct uthread));
// 初始化栈指针
char *stack_top = uthread->stack + STACK_SIZE;
uthread->context.rsp = (long long)stack_top - 8; // 假设我们需要留出8字节的空间
uthread->context.rip = (long long)_uthread_entry;
uthread->context.rdi = (long long)uthread;
uthread->context.rsi = (long long)func;
uthread->context.rdx = (long long)arg;
uthread->state = THREAD_INIT;
uthread->name = thread_name;
thread_queue[thread_count++] = uthread;
return uthread;
}
void schedule() {
if (setjmp(main_context) == 0) { // 初次调用或从上一个线程返回
while (thread_count > 0) {
current_thread = thread_queue[current_index];
if (current_thread->state == THREAD_STOP) {
// 移除停止的线程
free(current_thread);
for (int i = current_index; i < thread_count - 1; i++) {
thread_queue[i] = thread_queue[i + 1];
}
thread_queue[--thread_count] = NULL;
if (current_index >= thread_count) {
current_index = 0;
}
continue;
}
if (current_thread->state == THREAD_INIT) {
current_thread->state = THREAD_RUNNING;
if (!setjmp(main_context)) {
thread_switch(&main_thread->context, ¤t_thread->context);
}
} else {
uthread_resume(current_thread);
}
current_index = (current_index + 1) % thread_count;
}
}
}
void uthread_resume(struct uthread *tcb) {
if (setjmp(main_context) == 0) {
current_thread = tcb;
thread_switch(&main_thread->context, &tcb->context);
}
}
long long uthread_yield() {
current_thread->state = THREAD_SUSPENDED;
if (!setjmp(main_context)) {
thread_switch(¤t_thread->context, &main_thread->context);
}
return 0;
}
// 假设的上下文切换函数,实际实现依赖于具体的架构和编译器
void init_uthreads() {
main_thread = malloc(sizeof(struct uthread));
if (!main_thread) {
printf("Error allocating memory for main thread\n");
exit(1);
}
memset(main_thread, 0, sizeof(struct uthread));
main_thread->state = THREAD_RUNNING;
current_thread = main_thread;
}
void uthread_destroy(struct uthread *tcb) {
free(tcb);
}
2.在vscode上先暂存再commit
commit
3.生成patch
查看一下分支树
对origin/lab1生成patch
git format-patch origin/lab1
然后将0001-try2.patch上传提交即可
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构