From d8eaa13f2efc1eeca4e5f230d5a2d535aaf27338 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 2 Jan 2026 00:47:16 +0800 Subject: dev(file-system): establish VFS and Read-only FAT32 File System --- src/kernel/proc/proc.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/kernel/proc/proc.cc') diff --git a/src/kernel/proc/proc.cc b/src/kernel/proc/proc.cc index c8b6d1d..f1f1c1d 100644 --- a/src/kernel/proc/proc.cc +++ b/src/kernel/proc/proc.cc @@ -8,6 +8,8 @@ #include "kernel/trap.h" #include "kernel/riscv.h" #include "drivers/uart.h" +#include "fs/fs.h" +#include "fs/file.h" #include "lib/string.h" #define NPROC 64 @@ -172,6 +174,9 @@ found: p->pid = nextpid++; p->priority = 0; p->cpu = mycpu(); + p->cwd = nullptr; + for (int i = 0; i < NOFILE; i++) + p->ofile[i] = nullptr; if ((p->tf = (struct Trapframe *)PMM::alloc_page()) == 0) { @@ -206,6 +211,19 @@ static void freeproc(struct Proc *p) p->kstack = 0; if (p->pagetable) VM::uvmfree(p->pagetable, p->sz); + for (int i = 0; i < NOFILE; i++) + { + if (p->ofile[i]) + { + FileTable::close(p->ofile[i]); + p->ofile[i] = nullptr; + } + } + if (p->cwd) + { + VFS::iput(p->cwd); + p->cwd = nullptr; + } p->pagetable = 0; p->sz = 0; p->pid = 0; @@ -245,6 +263,19 @@ int fork() np->parent = p; np->priority = p->priority; + for (int i = 0; i < NOFILE; i++) + { + if (p->ofile[i]) + { + np->ofile[i] = FileTable::dup(p->ofile[i]); + } + } + + if (p->cwd) + { + np->cwd = VFS::idup(p->cwd); + } + proc_mem_lock.release(); struct cpu *c = mycpu(); @@ -394,6 +425,8 @@ namespace ProcManager p->sz = stack_va + PGSIZE; strcpy(p->name, "initcode"); + p->cwd = nullptr; + proc_mem_lock.release(); struct cpu *c = &cpus[0]; -- cgit v1.2.3