文章目录
OSlab3学习笔记
实验目的
-
创建一个进程并成功运行
-
实现时钟中断,通过时钟中断内核可以再次获得执行权
-
实现进程调度,创建两个进程,并且通过时钟中断切换进程执行
-
在本次实验中你将运行一个用户模式的进程。你需要使用数据结构进程控制块 Env 来跟踪用户进程。通过建立一个简单的用户进程,加载一个程序镜像到进程控制块中,并让它运行起来。同时,你的MIPS 内核将拥有处理异常的能力。
重点定义
env.h
#define LOG2NENV 10
#define NENV (1<<LOG2NENV)//envs数组包含NENV个Env结构体成员
#define ENVX(envid) ((envid) & (NENV - 1))//得到envid的低10位
types.h
#define ROUNDDOWN(a,n) (((u_long)(a)) & ~((n)-1))
//当n是2的次方时,这个宏的作用是将a的低log2n位清零
PCB结构
进程控制块(PCB) 是系统为了管理进程设置的一个专门的数据结构,用它来记录进程的外部特征,描述进程的运动变化过程。进程和PCB是一一对应的,PCB的结构如下。
struct Env {
struct Trapframe env_tf; // Saved registers
LIST_ENTRY(Env) env_link; // Free LIST_ENTRY
/*
the structure of env_link:
struct{
struct Env *le_next;
struct Env **le_prev;
}
使用它和env_free_list来构造空闲进程链表。
*/
u_int env_id; // Unique environment identifier
u_int env_parent_id; // env_id of this env's parent
u_int env_status; // Status of the environment
/*
– ENV_FREE : 表明该进程是不活动的,即该进程控制块处于进程空闲链表中。
– ENV_NOT_RUNNABLE : 表明该进程处于阻塞状态,处于该状态的进程往往在等待一定的条件才可以变为就绪状态从而被CPU调度。
– ENV_RUNNABLE : 表明该进程处于就绪状态,正在等待被调度,但处于RUNNABLE状态的进程可以是正在运行的,也可能不在运行中。
*/
Pde *env_pgdir; // Kernel virtual address of page dir
u_int env_cr3; // physical address of page
LIST_ENTRY(Env) env_sched_link;// use it to construct processes in ENV_RUNNABLE status.
u_int env_pri; // the priority of the process
};
Trapeframe
struct Trapframe {
//lr:need to be modified(reference to linux pt_regs) TODO
unsigned long regs[32];