#include <proc.h>
#include <kmalloc.h>
#include <string.h>
#include <sync.h>
#include <pmm.h>
#include <error.h>
#include <sched.h>
#include <elf.h>
#include <vmm.h>
#include <trap.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <fs.h>
#include <vfs.h>
#include <sysfile.h>
/* ------------- process/thread mechanism design&implementation -------------
(an simplified Linux process/thread mechanism )
introduction:
ucore implements a simple process/thread mechanism. process contains the independent memory sapce, at least one threads
for execution, the kernel data(for management), processor state (for context switch), files(in lab6), etc. ucore needs to
manage all these details efficiently. In ucore, a thread is just a special kind of process(share process's memory).
------------------------------
process state : meaning -- reason
PROC_UNINIT : uninitialized -- alloc_proc
PROC_SLEEPING : sleeping -- try_free_pages, do_wait, do_sleep
PROC_RUNNABLE : runnable(maybe running) -- proc_init, wakeup_proc,
PROC_ZOMBIE : almost dead -- do_exit
-----------------------------
process state changing:
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----------------------------------
-----------------------------
process relations
parent: proc->parent (proc is children)
children: proc->cptr (proc is parent)
older sibling: proc->optr (proc is younger sibling)
younger sibling: proc->yptr (proc is older sibling)
-----------------------------
related syscall for process:
SYS_exit : process exit, -->do_exit
SYS_fork : create child process, dup mm -->do_fork-->wakeup_proc
SYS_wait : wait process -->do_wait
SYS_exec : after fork, process execute a program -->load a program and refresh the mm
SYS_clone : create child thread -->do_fork-->wakeup_proc
SYS_yield : process flag itself need resecheduling, -- proc->need_sched=1, then scheduler will rescheule this process
SYS_sleep : process sleep -->do_sleep
SYS_kill : kill process -->do_kill-->proc->flags |= PF_EXITING
-->wakeup_proc-->do_wait-->do_exit
SYS_getpid : get the process's pid
*/
// the process set's list
list_entry_t proc_list;
#define HASH_SHIFT 10
#define HASH_LIST_SIZE (1 << HASH_SHIFT)
#define pid_hashfn(x) (hash32(x, HASH_SHIFT))
// has list for process set based on pid
static list_entry_t hash_list[HASH_LIST_SIZE];
// idle proc
struct proc_struct *idleproc = NULL;
// init proc
struct proc_struct *initproc = NULL;
// current proc
struct proc_struct *current = NULL;
static int nr_process = 0;
void kernel_thread_entry(void);
void forkrets(struct trapframe *tf); // 调用 trap/trapentry.S 中的 forkrets:
void switch_to(struct context *from, struct context *to); // 调用switch.S
// alloc_proc - alloc a proc_struct and init all fields of proc_struct
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
* 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
*/
//LAB5 YOUR CODE : (update LAB4 steps)
/*
* below fields(add in LAB5) in proc_struct need to be initialized
* uint32_t wait_state; // waiting state
* struct proc_struct *cptr, *yptr, *optr; // relations between processes
*/
//LAB6 YOUR CODE : (update LAB5 steps)
/*
* below fields(以下字段)(add in LAB6) in proc_struct need to be initialized(初始化)
* 当然,这些字段都在 proc.h 头文件中的 proc_struct 结构体中定义过了。
* struct run_queue *rq; // running queue contains Process(运行队列)
* list_entry_t run_link; // the entry linked in run queue(运行队列指针)
* int time_slice; // time slice for occupying the CPU(时间片)
* skew_heap_entry_t lab6_run_pool; // FOR LAB6 ONLY: the entry in the run pool
* uint32_t lab6_stride; // FOR LAB6 ONLY: the current stride of the process
* uint32_t lab6_priority; // FOR LAB6 ONLY: the priority of process, set by lab6_set_priority(uint32_t)
*/
//LAB8:EXERCISE2 YOUR CODE HINT:need add some code to init fs in proc_struct, ...
/*
* 执行的是第一步初始化工作
*/
proc->state = PROC_UNINIT; // 设置了进程的状态为“初始”态,这表示进程已经 “出生”了,正在获取资源茁壮成长中;
proc->pid = -1; // 未分配的进程pid是-1 先设置pid为无效值-1,用户调完alloc_proc函数后再根据实际情况设置pid。
// 设置了进程的pid为-1,这表示进程的“身份证号”还没有办好;
proc->cr3 = boot_cr3; // boot_cr3指向了uCore启动时建立好的内核虚拟空间的页目录表首地址
// 表明由于该内核线程在内核中运行,故采用为uCore内核已经建立的页表,即设置为在uCore内核页表的起始地址boot_cr3。
proc->runs = 0;
proc->kstack = 0; // 记录了分配给该进程/线程的内核栈的位置
proc->need_resched = 0; // 是否需要调度
proc->parent = NULL; // 父进程
proc->mm = 0; // 虚拟内存结构体(lab4实验可忽略)
/*
* void *memset(void *s, int c, unsigned long n)
* 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s
* 该函数用于清空一个结构体中所有的成员变量,下面解释三个参数:
* 第一个参数:位置指针,例如数组名、结构体首地址
* 第二个参数:替换为什么
* memset 函数的第�
没有合适的资源?快使用搜索试试~ 我知道了~
清华大学操作系统课程实验 作业.zip
共1301个文件
c:331个
h:295个
o:170个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 18 浏览量
2024-04-20
17:26:58
上传
评论
收藏 28.99MB ZIP 举报
温馨提示
清华大学操作系统课程实验 作业.zip
资源推荐
资源详情
资源评论
收起资源包目录
清华大学操作系统课程实验 作业.zip (1301个子文件)
kernel.asm 1.04MB
kernel.asm 783KB
kernel.asm 710KB
kernel.asm 482KB
kernel_nopage.asm 479KB
kernel.asm 290KB
matrix.asm 116KB
priority.asm 110KB
waitkill.asm 103KB
exit.asm 102KB
testbss.asm 102KB
badarg.asm 102KB
forktest.asm 102KB
spin.asm 102KB
sleep.asm 101KB
forktree.asm 101KB
sleepkill.asm 100KB
yield.asm 100KB
hello.asm 98KB
divzero.asm 98KB
pgdir.asm 98KB
faultreadkernel.asm 98KB
faultread.asm 98KB
badsegment.asm 98KB
softint.asm 98KB
bootblock.asm 16KB
bootblock.asm 16KB
bootblock.asm 16KB
bootblock.asm 16KB
bootblock.asm 15KB
bootblock 512B
bootblock 512B
bootblock 512B
bootblock 512B
bootblock 512B
proc.c 43KB
proc.c 37KB
proc.c 35KB
proc.c 33KB
sfs_inode.c 32KB
pmm.c 28KB
pmm.c 27KB
pmm.c 27KB
pmm.c 27KB
pmm.c 23KB
pmm.c 23KB
pmm.c 22KB
proc.c 19KB
vmm.c 19KB
mksfs.c 19KB
vmm.c 18KB
vmm.c 18KB
vmm.c 18KB
vmm.c 14KB
vmm.c 14KB
kdebug.c 14KB
kdebug.c 14KB
kdebug.c 14KB
kdebug.c 14KB
kdebug.c 12KB
kdebug.c 12KB
kdebug.c 12KB
kdebug.c 12KB
console.c 12KB
console.c 12KB
console.c 12KB
console.c 12KB
console.c 12KB
console.c 12KB
console.c 12KB
console.c 12KB
default_pmm.c 11KB
default_pmm.c 11KB
default_pmm.c 11KB
default_pmm.c 11KB
default_pmm.c 11KB
default_pmm.c 11KB
default_pmm.c 11KB
string.c 11KB
string.c 11KB
string.c 11KB
string.c 11KB
string.c 11KB
string.c 11KB
string.c 11KB
string.c 11KB
printfmt.c 10KB
trap.c 10KB
trap.c 10KB
trap.c 10KB
printfmt.c 10KB
printfmt.c 10KB
printfmt.c 10KB
trap.c 10KB
printfmt.c 10KB
printfmt.c 10KB
printfmt.c 10KB
printfmt.c 10KB
file.c 9KB
vfsdev.c 8KB
共 1301 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
小码蚁.
- 粉丝: 2525
- 资源: 4090
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功