rename Process member fields and remove name

This commit is contained in:
Garen Tyler 2023-11-03 21:54:51 -06:00
parent 954db4c998
commit 54753062e4
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
7 changed files with 41 additions and 56 deletions

View File

@ -87,6 +87,7 @@ struct proc *myproc();
void procinit(void);
void sleep_lock(void *, struct spinlock *);
void userinit(void);
void wakeup(void *chan);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);

View File

@ -22,7 +22,7 @@ int flags2perm(int flags)
int
exec(char *path, char **argv)
{
char *s, *last;
char *s;
int i, off;
uint64 argc, sz = 0, sp, ustack[MAXARG], stackbase;
struct elfhdr elf;
@ -115,11 +115,9 @@ exec(char *path, char **argv)
p->trapframe->a1 = sp;
// Save program name for debugging.
for(last=s=path; *s; s++)
if(*s == '/')
last = s+1;
safestrcpy(p->name, last, sizeof(p->name));
for (s = path; *s; s++)
;
// Commit to the user image.
oldpagetable = p->pagetable;
p->pagetable = pagetable;

View File

@ -96,7 +96,6 @@ userinit(void)
p->trapframe->epc = 0; // user program counter
p->trapframe->sp = PGSIZE; // user stack pointer
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
p->state = RUNNABLE;
@ -187,8 +186,6 @@ void procdump(void)
printint(p->pid);
printstr(" ");
printstr(state);
printstr(" ");
printstr(p->name);
printstr("\n");
}
}

View File

@ -106,5 +106,4 @@ struct proc {
struct context context; // swtch() here to run process
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
};

View File

@ -91,7 +91,7 @@ pub struct Process {
/// If non-zero, have been killed
pub killed: i32,
/// Exit status to be returned to parent's wait
pub xstate: i32,
pub exit_status: i32,
/// Process ID
pub pid: i32,
@ -101,9 +101,9 @@ pub struct Process {
// These are private to the process, so p->lock need not be held.
/// Virtual address of kernel stack
pub kstack: u64,
pub kernel_stack: u64,
/// Size of process memory (bytes)
pub sz: u64,
pub memory_allocated: u64,
/// User page table
pub pagetable: Pagetable,
/// Data page for trampoline.S
@ -111,11 +111,9 @@ pub struct Process {
/// swtch() here to run process
pub context: Context,
/// Open files
pub ofile: [*mut File; crate::NOFILE],
pub open_files: [*mut File; crate::NOFILE],
/// Current directory
pub cwd: *mut Inode,
/// Process name (debugging)
pub name: [c_char; 16],
pub current_dir: *mut Inode,
}
impl Process {
pub const fn new() -> Process {
@ -124,17 +122,16 @@ impl Process {
state: ProcessState::Unused,
chan: null_mut(),
killed: 0,
xstate: 0,
exit_status: 0,
pid: 0,
parent: null_mut(),
kstack: 0,
sz: 0,
kernel_stack: 0,
memory_allocated: 0,
pagetable: null_mut(),
trapframe: null_mut(),
context: Context::new(),
ofile: [null_mut(); crate::NOFILE],
cwd: null_mut(),
name: [0x00; 16],
open_files: [null_mut(); crate::NOFILE],
current_dir: null_mut(),
}
}
pub fn current() -> Option<&'static mut Process> {
@ -200,7 +197,7 @@ impl Process {
core::mem::size_of::<Context>() as u32,
);
p.context.ra = forkret as usize as u64;
p.context.sp = p.kstack + PGSIZE;
p.context.sp = p.kernel_stack + PGSIZE;
Ok(p)
}
@ -213,22 +210,21 @@ impl Process {
}
self.trapframe = null_mut();
if !self.pagetable.is_null() {
proc_freepagetable(self.pagetable, self.sz);
proc_freepagetable(self.pagetable, self.memory_allocated);
}
self.pagetable = null_mut();
self.sz = 0;
self.memory_allocated = 0;
self.pid = 0;
self.parent = null_mut();
self.name[0] = 0;
self.chan = null_mut();
self.killed = 0;
self.xstate = 0;
self.exit_status = 0;
self.state = ProcessState::Unused;
}
/// Grow or shrink user memory.
pub unsafe fn grow_memory(&mut self, num_bytes: i32) -> Result<(), ProcessError> {
let mut size = self.sz;
let mut size = self.memory_allocated;
if num_bytes > 0 {
size = uvmalloc(
@ -245,7 +241,7 @@ impl Process {
size = uvmdealloc(self.pagetable, size, size.wrapping_add(num_bytes as u64));
}
self.sz = size;
self.memory_allocated = size;
Ok(())
}
@ -304,12 +300,12 @@ impl Process {
let child = Process::alloc()?;
// Copy user memory from parent to child.
if uvmcopy(parent.pagetable, child.pagetable, parent.sz) < 0 {
if uvmcopy(parent.pagetable, child.pagetable, parent.memory_allocated) < 0 {
child.free();
child.lock.unlock();
return Err(ProcessError::Allocation);
}
child.sz = parent.sz;
child.memory_allocated = parent.memory_allocated;
// Copy saved user registers.
*child.trapframe = *parent.trapframe;
@ -318,18 +314,12 @@ impl Process {
(*child.trapframe).a0 = 0;
// Increment reference counts on open file descriptors.
for (i, file) in parent.ofile.iter().enumerate() {
for (i, file) in parent.open_files.iter().enumerate() {
if !file.is_null() {
child.ofile[i] = filedup(parent.ofile[i]);
child.open_files[i] = filedup(parent.open_files[i]);
}
}
child.cwd = idup(parent.cwd);
safestrcpy(
addr_of!(child.name[0]).cast_mut().cast(),
addr_of!(parent.name[0]).cast_mut().cast(),
parent.name.len() as i32,
);
child.current_dir = idup(parent.current_dir);
let pid = child.pid;
@ -366,7 +356,7 @@ impl Process {
}
// Close all open files.
for file in self.ofile.iter_mut() {
for file in self.open_files.iter_mut() {
if !file.is_null() {
fileclose(*file);
*file = null_mut();
@ -375,9 +365,9 @@ impl Process {
{
let _operation = LogOperation::new();
iput(self.cwd);
iput(self.current_dir);
}
self.cwd = null_mut();
self.current_dir = null_mut();
{
let _guard = wait_lock.lock();
@ -389,7 +379,7 @@ impl Process {
wakeup(self.parent.cast());
self.lock.lock_unguarded();
self.xstate = status;
self.exit_status = status;
self.state = ProcessState::Zombie;
}
@ -421,7 +411,7 @@ impl Process {
&& copyout(
self.pagetable,
addr,
addr_of_mut!(p.xstate).cast(),
addr_of_mut!(p.exit_status).cast(),
core::mem::size_of::<i32>() as u64,
) < 0
{

View File

@ -121,8 +121,8 @@ impl Syscall {
return -1i64 as u64;
}
fs::iunlock(inode);
fs::iput(proc.cwd);
proc.cwd = inode;
fs::iput(proc.current_dir);
proc.current_dir = inode;
0
}
Syscall::Dup => {
@ -144,7 +144,7 @@ impl Syscall {
let mut n = 0i32;
argint(0, addr_of_mut!(n));
let proc = Process::current().unwrap();
let addr = proc.sz;
let addr = proc.memory_allocated;
if unsafe { proc.grow_memory(n).is_ok() } {
addr
@ -193,7 +193,7 @@ impl Syscall {
let mut file: *mut File = null_mut();
if argfd(0, addr_of_mut!(file_descriptor), addr_of_mut!(file)) >= 0 {
Process::current().unwrap().ofile[file_descriptor as usize] = null_mut();
Process::current().unwrap().open_files[file_descriptor as usize] = null_mut();
file::fileclose(file);
0
} else {
@ -274,8 +274,8 @@ pub unsafe extern "C" fn fetchaddr(addr: u64, ip: *mut u64) -> i32 {
let proc = Process::current().unwrap();
// Both tests needed, in case of overflow.
if addr >= proc.sz
|| addr + size_of::<u64>() as u64 > proc.sz
if addr >= proc.memory_allocated
|| addr + size_of::<u64>() as u64 > proc.memory_allocated
|| copyin(
proc.pagetable,
ip.cast(),
@ -309,8 +309,8 @@ unsafe fn fdalloc(file: *mut File) -> Result<usize, ()> {
let proc = Process::current().unwrap();
for file_descriptor in 0..crate::NOFILE {
if proc.ofile[file_descriptor].is_null() {
proc.ofile[file_descriptor] = file;
if proc.open_files[file_descriptor].is_null() {
proc.open_files[file_descriptor] = file;
return Ok(file_descriptor);
}
}
@ -359,7 +359,7 @@ pub unsafe extern "C" fn argfd(
return -1;
}
let file: *mut File = Process::current().unwrap().ofile[file_descriptor];
let file: *mut File = Process::current().unwrap().open_files[file_descriptor];
if file.is_null() {
return -1;
}

View File

@ -145,7 +145,7 @@ pub unsafe extern "C" fn usertrapret() {
// kernel page table
(*proc.trapframe).kernel_satp = r_satp();
// process's kernel stack
(*proc.trapframe).kernel_sp = proc.kstack + PGSIZE;
(*proc.trapframe).kernel_sp = proc.kernel_stack + PGSIZE;
(*proc.trapframe).kernel_trap = usertrap as usize as u64;
// hartid for Cpu::current_id()
(*proc.trapframe).kernel_hartid = r_tp();