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

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

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

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. Element-UI资源原型库

    Element-UI v2.0.0版本 Axure和Sketch库: 链接:https://pan.baidu.com/s/1LdsEh8BKQBjcWBKV5yQilQ 提取码:1xqn

  2. 学习heartbeat-05 实现web服务高可用

    一.环境介绍 说明:所有案例在虚拟机(VMware)上完成 操作系统:centos 6.5 64bit 高可用软件:heartbeat 3.0.4 Web应用服务器:apache httpd 2.2. ...

  3. CEPH-3:cephfs功能详解

    ceph集群cephfs使用详解 一个完整的ceph集群,可以提供块存储.文件系统和对象存储. 本节主要介绍文件系统cephfs功能如何灵活的使用,集群背景: [cephadmin@yq01-aip- ...

  4. vue 3d轮播组件 vue-carousel-3d

    开发可视化项目时,需要3d轮播图,找来找去发现这个组件,引用简单,最后实现效果还不错.发现关于这个组件,能搜到的教程不多,就分享一下我的经验. 插件github地址:https://wlada.git ...

  5. Netty学习摘记 —— UDP广播事件

    本文参考 本篇文章是对<Netty In Action>一书第十三章"使用UDP广播事件"的学习摘记,主要内容为广播应用程序的开发 消息POJO 我们将日志信息封装成名 ...

  6. 1kb 的 placeholder.js 增加 img 标签使用方式

    预览 官方网站示例. 项目 github 地址 使用 引入 placeholder.js 到你的前段代码中: <script src="placeholder.js"> ...

  7. 移动端城市定位,城市区域代码adcode

    使用高德定位API : AMap.Map('iCenter') AMap.CitySearch() 先在高德开放平台注册申请定位权限的key. 网站:高德开放平台 在需要定位的页面引入有定位key的s ...

  8. Jenkins 脚本命令行应用总结

    Jenkins脚本命令行应用总结 测试环境 Jenkins 2.304 脚本命令行入口 Jenkins主页→系统管理→脚本命令行 遍历项目 例子:获取所有自由风格项目及相关项目信息 def proje ...

  9. python---用顺序表实现双端队列

    class Dqueue(object): """双端队列""" def __init__(self): self.__list = [] ...

  10. Spring配置文件-Bean生命周期配置(init-method方法,destory-method方法)

    1.UserDaoImpl类 public class UserDaoImpl implements UserDao { public UserDaoImpl(){ System.out.printl ...