add user app: forkexec.rs, update hello_world.rs

This commit is contained in:
Yu Chen 2022-03-29 16:37:55 +08:00
parent 07bdb0ce7e
commit 3bc09a651e
2 changed files with 30 additions and 1 deletions

27
user/src/bin/forkexec.rs Normal file
View File

@ -0,0 +1,27 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, getpid, wait, exec};
#[no_mangle]
pub fn main() -> i32 {
println!("pid {}: parent start forking ...", getpid());
let pid = fork();
if pid == 0 {
// child process
println!("pid {}: forked child start execing hello_world app ... ", getpid());
exec("hello_world");
100
} else {
// parent process
let mut exit_code: i32 = 0;
println!("pid {}: ready waiting child ...", getpid());
assert_eq!(pid, wait(&mut exit_code));
assert_eq!(exit_code, 0);
println!("pid {}: got child info:: pid {}, exit code: {}", getpid() , pid, exit_code);
0
}
}

View File

@ -4,8 +4,10 @@
#[macro_use]
extern crate user_lib;
use user_lib::{getpid};
#[no_mangle]
pub fn main() -> i32 {
println!("Hello world from user mode program!");
println!("pid {}: Hello world from user mode program!", getpid());
0
}