|
|
@ -13,16 +13,28 @@ pub fn sys_fork(tf: &TrapFrame) -> SysResult {
|
|
|
|
/// Wait the process exit.
|
|
|
|
/// Wait the process exit.
|
|
|
|
/// Return the PID. Store exit code to `code` if it's not null.
|
|
|
|
/// Return the PID. Store exit code to `code` if it's not null.
|
|
|
|
pub fn sys_wait(pid: usize, code: *mut i32) -> SysResult {
|
|
|
|
pub fn sys_wait(pid: usize, code: *mut i32) -> SysResult {
|
|
|
|
|
|
|
|
info!("wait: pid {} code {:?}", pid, code);
|
|
|
|
|
|
|
|
if !code.is_null() {
|
|
|
|
process().memory_set.check_mut_ptr(code)?;
|
|
|
|
process().memory_set.check_mut_ptr(code)?;
|
|
|
|
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
use alloc::vec;
|
|
|
|
use alloc::vec;
|
|
|
|
let wait_procs = match pid {
|
|
|
|
let wait_procs = match pid {
|
|
|
|
0 => processor().manager().get_children(thread::current().id()),
|
|
|
|
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() {
|
|
|
|
if wait_procs.is_empty() {
|
|
|
|
return Ok(-1);
|
|
|
|
return Err(SysError::ECHILD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for pid in wait_procs {
|
|
|
|
for pid in wait_procs {
|
|
|
|
match processor().manager().get_status(pid) {
|
|
|
|
match processor().manager().get_status(pid) {
|
|
|
|
Some(Status::Exited(exit_code)) => {
|
|
|
|
Some(Status::Exited(exit_code)) => {
|
|
|
@ -31,7 +43,7 @@ pub fn sys_wait(pid: usize, code: *mut i32) -> SysResult {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
processor().manager().remove(pid);
|
|
|
|
processor().manager().remove(pid);
|
|
|
|
info!("wait: {} -> {}", thread::current().id(), pid);
|
|
|
|
info!("wait: {} -> {}", thread::current().id(), pid);
|
|
|
|
return Ok(0);
|
|
|
|
return Ok(pid as isize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => return Ok(-1),
|
|
|
|
None => return Ok(-1),
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|