forktree worked with depth=3 on k210 platform.

This commit is contained in:
Yifan Wu 2020-12-11 16:14:14 +08:00
parent 48a11e53ff
commit a3496698b3
6 changed files with 62 additions and 17 deletions

View file

@ -30,6 +30,6 @@ pub fn main() -> i32 {
assert_eq!(xstate, 0);
}
assert!(wait(&mut xstate) < 0);
println!("r_forktest2 test passed!");
println!("forktest2 test passed!");
0
}

37
user/src/bin/forktree.rs Normal file
View file

@ -0,0 +1,37 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{sleep, getpid, fork, exit, yield_};
const DEPTH: usize = 3;
fn fork_child(cur: &str, branch: char) {
let mut next = [0u8; DEPTH + 1];
let l = cur.len();
if l >= DEPTH {
return;
}
&mut next[..l].copy_from_slice(cur.as_bytes());
next[l] = branch as u8;
if fork() == 0 {
fork_tree(core::str::from_utf8(&next[..l + 1]).unwrap());
yield_();
exit(0);
}
}
fn fork_tree(cur: &str) {
println!("pid{}: {}", getpid(), cur);
fork_child(cur, '0');
fork_child(cur, '1');
}
#[no_mangle]
pub fn main() -> i32 {
fork_tree("");
sleep(3000);
0
}

View file

@ -17,13 +17,17 @@ fn main() -> i32 {
exec("user_shell\0");
} else {
loop {
let mut xstatus: i32 = 0;
let pid = wait(&mut xstatus);
let mut exit_code: i32 = 0;
let pid = wait(&mut exit_code);
if pid == -1 {
yield_();
continue;
}
println!("[initproc] Release a zombie process!");
println!(
"[initproc] Released a zombie process, pid={}, exit_code={}",
pid,
exit_code,
);
}
}
0