From 8809c6ca7f72f046e4df5281b85805b6d45fe819 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Thu, 7 Mar 2019 23:32:47 +0800 Subject: [PATCH] Improve sys_wait and replace CR with LF --- kernel/src/syscall/proc.rs | 20 ++++++++++++++++---- kernel/src/trap.rs | 7 ++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index 0e776df..af438e7 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -13,16 +13,28 @@ pub fn sys_fork(tf: &TrapFrame) -> SysResult { /// Wait the process exit. /// Return the PID. Store exit code to `code` if it's not null. pub fn sys_wait(pid: usize, code: *mut i32) -> SysResult { - process().memory_set.check_mut_ptr(code)?; + info!("wait: pid {} code {:?}", pid, code); + if !code.is_null() { + process().memory_set.check_mut_ptr(code)?; + } loop { use alloc::vec; let wait_procs = match pid { 0 => processor().manager().get_children(thread::current().id()), - _ => vec![pid], + // check if pid is a child + _ => { + if processor().manager().get_children(thread::current().id()).iter() + .find(|p| **p == pid).is_some() { + vec![pid] + } else { + vec![] + } + } }; if wait_procs.is_empty() { - return Ok(-1); + return Err(SysError::ECHILD); } + for pid in wait_procs { match processor().manager().get_status(pid) { Some(Status::Exited(exit_code)) => { @@ -31,7 +43,7 @@ pub fn sys_wait(pid: usize, code: *mut i32) -> SysResult { } processor().manager().remove(pid); info!("wait: {} -> {}", thread::current().id(), pid); - return Ok(0); + return Ok(pid as isize); } None => return Ok(-1), _ => {} diff --git a/kernel/src/trap.rs b/kernel/src/trap.rs index bff1c57..8b42121 100644 --- a/kernel/src/trap.rs +++ b/kernel/src/trap.rs @@ -27,5 +27,10 @@ pub fn error(tf: &TrapFrame) -> ! { } pub fn serial(c: char) { - crate::fs::STDIN.push(c); + if c == '\r' { + // in linux, we use '\n' instead + crate::fs::STDIN.push('\n'); + } else { + crate::fs::STDIN.push(c); + } } \ No newline at end of file