练习一:分配并初始化一个进程控制块(需要编码)
alloc_proc函数(位于kern/process/proc.c中)负责分配并返回一个新的struct proc_struct结构,用于存储新建立的内核线程的管理信息。ucore需要对这个结构进行最基本的初始化,完成这个初始化过程。
【提示】在alloc_proc函数的实现中,需要初始化的proc_struct结构中的成员变量至少包括:state/pid/runs/kstack/need_resched/parent/mm/context/tf/cr3/flags/name。
请说明proc_struct中struct context context和struct trapframe *tf成员变量含义和在本实验中的作用是啥?(提示通过看代码和编程调试可以判断出来)
1、实现:
static struct proc_struct *
alloc_proc(void) {
struct proc_struct *proc = kmalloc(sizeof(struct proc_struct));
if (proc != NULL) {
//LAB4:EXERCISE1 YOUR CODE
/*
* below fields in proc_struct need to be initialized
* 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
*

该博客详细介绍了ucore操作系统实验中关于内核线程管理的内容,包括分配初始化进程控制块、资源分配、进程上下文切换及PID的唯一性。文章阐述了alloc_proc函数的作用,分析了context和tf成员变量在进程切换中的意义,讨论了do_fork函数的实现步骤,并解释了proc_run函数在进程切换中的功能。实验中,系统创建了idleproc和initproc两个内核线程,local_intr_save和local_intr_restore用于保护进程切换过程不受中断影响。
最低0.47元/天 解锁文章
2956

被折叠的 条评论
为什么被折叠?



