From 3bc09a651ecc98310af0d8aed3aca6f1d005b9ff Mon Sep 17 00:00:00 2001 From: Yu Chen Date: Tue, 29 Mar 2022 16:37:55 +0800 Subject: [PATCH] add user app: forkexec.rs, update hello_world.rs --- user/src/bin/forkexec.rs | 27 +++++++++++++++++++++++++++ user/src/bin/hello_world.rs | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 user/src/bin/forkexec.rs diff --git a/user/src/bin/forkexec.rs b/user/src/bin/forkexec.rs new file mode 100644 index 00000000..4f55f205 --- /dev/null +++ b/user/src/bin/forkexec.rs @@ -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 + } +} diff --git a/user/src/bin/hello_world.rs b/user/src/bin/hello_world.rs index 10d3f26c..4b370c20 100644 --- a/user/src/bin/hello_world.rs +++ b/user/src/bin/hello_world.rs @@ -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 }