越学越简单,真是越学越简单啊

看视频的时候着实被那复杂的函数调用图吓到了.看代码的时候发现条理还是很清晰的,远没有没想象的那么复杂.

这节创建了俩内核线程,然后运行第一个线程,再由第一个切换到第二个.

kern_init:

在vmm_init后加了一个proc_init

在最末位加了个cpu_idel

proc.c&.h

枚举类proc_state定义了进程生命周期里的各种状态

// process's state in his life cycle
enum proc_state {
PROC_UNINIT = 0, // uninitialized
PROC_SLEEPING, // sleeping
PROC_RUNNABLE, // runnable(maybe running)
PROC_ZOMBIE, // almost dead, and wait parent proc to reclaim his resource
};

各状态间的转化:

  alloc_proc                                 RUNNING
+ +--<----<--+
+ + proc_run +
V +-->---->--+
PROC_UNINIT -- proc_init/wakeup_proc --> PROC_RUNNABLE -- try_free_pages/do_wait/do_sleep --> PROC_SLEEPING --
A + +
| +--- do_exit --> PROC_ZOMBIE +
+ +
-----------------------wakeup_proc----------------------------------

proc_struct,就课程里讲的那个进程控制块(PCB)

struct proc_struct {
enum proc_state state; // Process state
int pid; // Process ID
int runs; // the running times of Proces
uintptr_t kstack; // Process kernel stack
volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
struct proc_struct *parent; // the parent process
struct mm_struct *mm; // Process's memory management field
struct context context; // Switch here to run process
struct trapframe *tf; // Trap frame for current interrupt
uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
uint32_t flags; // Process flag
char name[PROC_NAME_LEN + 1]; // Process name
list_entry_t list_link; // Process link list
list_entry_t hash_link; // Process hash list
};

list_entry_t proc_list 链表形式的进程集合

list_entry_t hash_list[1024] 散列表形式的进程集合

proc_struct *idleproc 0号进程,作用是不断检查当前有无处于就绪状态的进程,有则立即运行

proc_struct *initproc 本实验中测试用的进程,打印一句 hello world

static int nr_process 线程计数器

alloc_proc:分配一个PCB并初始化

各种成员变量清零

state设为UNINIT

pid=-1

cr3=内核页目录表基址(物理地址)

kernel_thread(fn,arg,clone_flag) :创建内核线程

创建一个临时trapframe

CS,DS,SS,ES均取内核态的对应值

ebx=fn

edx=arg

eip=kernel_thread_entry //中断返回时从kernel_thread_entry继续

kernel_thread_entry在entry中定义:把arg做参数调用fn,把fn返回值做参数调用do_exit

调用do_fork(clone_flags|CLONE_VM,0,&tf)

do_fork: 根据tf,stack,clone_tf创建新线程

  1. 分配并初始化进程控制块(alloc_proc函数);
  2. 分配并初始化内核栈(setup_kstack函数,分配两个页当栈使唤);
  3. 根据clone_flag标志复制或共享进程内存管理结构(copy_mm函数,本实验不用mm,返回空);
  4. 设置进程在内核(将来也包括用户态)正常运行和调度所需的中断帧和执行上下文(copy_thread函数);
  5. 分配pid(get_pid函数)
  6. 把设置好的进程控制块放入hash_list和proc_list两个全局进程链表中;
  7. 自此,进程已经准备好执行了,把进程状态设置为“就绪”态;
  8. 设置返回码为子进程的id号。

copy_thread:

proc->tf = (struct trapframe *)(proc->kstack + KSTACKSIZE) - 1;
//在内核堆栈的顶部设置中断帧大小的一块栈空间
*(proc->tf) = *tf; //拷贝在kernel_thread函数建立的临时中断帧的初始值
proc->tf->tf_regs.reg_eax = 0;
//设置子进程/线程执行完do_fork后的返回值
proc->tf->tf_esp = esp; //设置中断帧中的栈指针esp
proc->tf->tf_eflags |= FL_IF; //使能中断
proc->context.eip = (uintptr_t)forkret; //trapentry.s定义,把esp压栈,调用__trapet
proc->context.esp = (uintptr_t)(proc->tf); //context.esp赋值为当前栈顶

get_pid:分配pid

这个比较难理解.last_pid=上一次分配的pid.当分配超过MAX_PID时从1开始重新分配

(last_pid,next_safe)指定了一段连续的未分配的pid区间.如果last_pid < next_safe时直接分配last_pid+1,否则以1为单位增加pid,每次增加都遍历整个proc_list查重,并更新next_safe,如果冲突了就再增1,从头再判断.

static int
get_pid(void) {
static_assert(MAX_PID > MAX_PROCESS);
struct proc_struct *proc;
list_entry_t *list = &proc_list, *le;
static int next_safe = MAX_PID, last_pid = MAX_PID;
if (++ last_pid >= MAX_PID) {
last_pid = 1;
goto inside;
}
if (last_pid >= next_safe) {
inside:
next_safe = MAX_PID;
repeat:
le = list;
while ((le = list_next(le)) != list) {
proc = le2proc(le, list_link);
if (proc->pid == last_pid) {
if (++ last_pid >= next_safe) {
if (last_pid >= MAX_PID) {
last_pid = 1;
}
next_safe = MAX_PID;
goto repeat;
}
}
else if (proc->pid > last_pid && next_safe > proc->pid) {
next_safe = proc->pid;
}
}
}
return last_pid;
}

proc_init:

void  proc_init(void) {
int i; //初始化proc_list和hash_list
list_init(&proc_list);
for (i = 0; i < HASH_LIST_SIZE; i ++) {
list_init(hash_list + i);
} //给idleproc分配一个PCB
if ((idleproc = alloc_proc()) == NULL) {
panic("cannot alloc idleproc.\n");
} idleproc->pid = 0; //设为0号进程
idleproc->state = PROC_RUNNABLE; //可运行
idleproc->kstack = (uintptr_t)bootstack; //kstack指向全句内核栈,在entry.S里用汇编定义,大小8KB
idleproc->need_resched = 1; //需要重新调度
set_proc_name(idleproc, "idle"); //命名
nr_process ++; //进程数+1 current = idleproc; int pid = kernel_thread(init_main, "Hello world!!", 0);
if (pid <= 0) {
panic("create init_main failed.\n");
} initproc = find_proc(pid);
set_proc_name(initproc, "init"); assert(idleproc != NULL && idleproc->pid == 0);
assert(initproc != NULL && initproc->pid == 1);
}

cpu_idle: 无限循环检查当前线程的need_resched,为真时调用schedule()

schedule:基于FIFO的调度算法

保存中断开关状态

从当前进程往后遍历,选择下一个RUNNABLE的进程调用proc_run

恢复中断开关状态

proc_run:

更新tss的特权态0下的栈顶指针esp0为新进程的栈顶

更新CR3位新进程页目录表物理地址,完成进程间页表切换

switch切换当前进程和新进程的上下文

switch_to:

.text
.globl switch_to
switch_to: # switch_to(from, to) # save from's registers
movl 4(%esp), %eax # eax points to from
popl 0(%eax) # save eip !popl
movl %esp, 4(%eax) # save esp::context of from
movl %ebx, 8(%eax) # save ebx::context of from
movl %ecx, 12(%eax) # save ecx::context of from
movl %edx, 16(%eax) # save edx::context of from
movl %esi, 20(%eax) # save esi::context of from
movl %edi, 24(%eax) # save edi::context of from
movl %ebp, 28(%eax) # save ebp::context of from # restore to's registers
movl 4(%esp), %eax # not 8(%esp): popped return address already
# eax now points to to
movl 28(%eax), %ebp # restore ebp::context of to
movl 24(%eax), %edi # restore edi::context of to
movl 20(%eax), %esi # restore esi::context of to
movl 16(%eax), %edx # restore edx::context of to
movl 12(%eax), %ecx # restore ecx::context of to
movl 8(%eax), %ebx # restore ebx::context of to
movl 4(%eax), %esp # restore esp::context of to pushl 0(%eax) # push eip ret

当调用switch_to(&(from->context), &(to->context)),进入它的第一行代码时,此时的栈布局为:

|to.context   |高地址
|from.context |
|ret address |<---esp

整个switch_to的功能为:

令eax=from.context

把各个寄存器保存到from.context里

令eax=to.context

把to.context恢复到各个寄存器里,把to.context.eip压栈

此时进行ret,栈顶出栈作为eip,返回到的地址就变成了to.context.eip,进程切换完成

对于进程init而言,我们前面把它的context.eip设为了forkret,具体功能为,把esp压栈,调用中断返回函数__trapet,进而将trapframe中的值恢复到的各个寄存器中.eip再次变更为trapframe.eip,即kernel_thread_entry函数,作用为把edx做参数调用ebx对应的函数,edx和ebx也在trapframe中分别指定为"hello world"和init_main,调用完init_main后再把返回值做参数调用do_exit.而do_exit负责退出进程,整个lab4内容结束.

ucore lab4 内核线程管理 学习笔记的更多相关文章

  1. ucore操作系统学习(四) ucore lab4内核线程管理

    1. ucore lab4介绍 什么是进程? 现代操作系统为了满足人们对于多道编程的需求,希望在计算机系统上能并发的同时运行多个程序,且彼此间互相不干扰.当一个程序受制于等待I/O完成等事件时,可以让 ...

  2. ucore lab5 用户进程管理 学习笔记

    近几日睡眠质量不佳,脑袋一困就没法干活,今天总算时补完了.LAB5难度比LAB4要高,想要理解所有细节时比较困难.但毕竟咱不是要真去写一个OS,所以一些个实现细节就当成黑箱略过了. 这节加上了用户进程 ...

  3. windows内核对象管理学习笔记

    目前正在阅读毛老师的<windows内核情景分析>一书对象管理章节,作此笔记. Win内核中是使用对象概念来描述管理内核中使用到的数据结构.此对象(Object)均是由对象头(Object ...

  4. C++内存管理学习笔记(5)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  5. C++内存管理学习笔记(6)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  6. C++内存管理学习笔记(7)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  7. Docker Image管理学习笔记,ZT

    Docker Image管理学习笔记 http://blog.csdn.net/junjun16818/article/details/38423391

  8. Linux内存管理学习笔记 转

    https://yq.aliyun.com/articles/11192?spm=0.0.0.0.hq1MsD 随着要维护的服务器增多,遇到的各种稀奇古怪的问题也会增多,要想彻底解决这些“小”问题往往 ...

  9. Linux内存管理学习笔记——内存寻址

    最近开始想稍微深入一点地学习Linux内核,主要参考内容是<深入理解Linux内核>和<深入理解Linux内核架构>以及源码,经验有限,只能分析出有限的内容,看完这遍以后再更深 ...

随机推荐

  1. 请说说你对Struts2的拦截器的理解?

    Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 拦截器栈(Interceptor Stac ...

  2. 部署新项目自动对数据库进行migrate和让用户收到创建用户/超级用户信息

    当项目中的models有数据表的时候,普通做法是用docke exec -it hello_web_1 bash,进入容器进行migrate,但是我们想要容器一启动就自动创建数据表,可以修改docke ...

  3. spring DAO 有什么用?

    Spring DAO 使得 JDBC,Hibernate 或 JDO 这样的数据访问技术更容易以一 种统一的方式工作.这使得用户容易在持久性技术之间切换.它还允许您在编写 代码时,无需考虑捕获每种技术 ...

  4. DateFormat类,利用SimpleDateFormat解决系统时间初始(格式化/解析)问题

    目标: java.text.DateFormat 是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进行来回转换. 格式化 ...

  5. stm32学习总结)—SPI-FLASH 实验 _

    SPI总线 SPI 简介 SPI 的全称是"Serial Peripheral Interface",意为串行外围接口,是Motorola 首先在其 MC68HCXX 系列处理器上 ...

  6. 【HTML5版】导出Table数据并保存为Excel

    首发我的博客 http://blog.meathill.com/tech/js/export-table-data-into-a-excel-file.html 最近接到这么个需求,要把<tab ...

  7. 微信小程序答题,怎么设计页面渲染,答完一题,跳到下一题

    想要的效果 1.第一页只显示第一道题的内容,如图红框2.答题后,点击下一题,内容显示第二道题的内容 代码 answer.wxml <!--pages/answer/answer.wxml--&g ...

  8. Tomcat安装(安装版)

    安装Tomcat(安装版) 下载地址https://tomcat.apache.org/ 下载成功,双击进行安装(一路Next). 等待安装结束. 然后打开浏览器输入地址:http://localho ...

  9. Java自定义异常类的简单实现

    学习目标: 掌握自定义异常类 例题: 需求:自定义异常类,简单判断是否注册成功 代码如下: RegisterException类: /** * @author YanYang * @projectNa ...

  10. localStorage存储返回过来的对象 显示object object的问题

    localStorage.setItem() 不会自动将Json对象转成字符串形式 用localStorage.setItem()正确存储JSON对象方法是: 存储前先用JSON.stringify( ...