wait child exit in forktree

This commit is contained in:
cl8192 2022-05-16 08:32:38 +08:00
parent 9a95c7dcb4
commit 7c83dd49e7

View File

@ -4,7 +4,7 @@
#[macro_use] #[macro_use]
extern crate user_lib; extern crate user_lib;
use user_lib::{exit, fork, getpid, sleep, yield_}; use user_lib::{exit, fork, getpid, sleep, yield_, wait};
const DEPTH: usize = 4; const DEPTH: usize = 4;
@ -21,18 +21,25 @@ fn fork_child(cur: &str, branch: char) {
yield_(); yield_();
exit(0); exit(0);
} }
exit(0);
} }
fn fork_tree(cur: &str) { fn fork_tree(cur: &str) {
println!("pid{}: {}", getpid(), cur); println!("pid{}: {}", getpid(), cur);
fork_child(cur, '0'); fork_child(cur, '0');
fork_child(cur, '1'); fork_child(cur, '1');
let mut exit_code: i32 = 0;
for _ in 0..2{
wait(&mut exit_code);
}
} }
#[no_mangle] #[no_mangle]
pub fn main() -> i32 { pub fn main() -> i32 {
fork_tree(""); fork_tree("");
let mut exit_code: i32 = 0;
for _ in 0..2{
wait(&mut exit_code);
}
sleep(3000); sleep(3000);
0 0
} }