|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +#[macro_use] |
| 5 | +extern crate user; |
| 6 | + |
| 7 | +use user::syscall::{sys_exit as exit, sys_yield as yield_now, sys_fork as fork, sys_wait as waitpid}; |
| 8 | + |
| 9 | +#[no_mangle] |
| 10 | +pub fn main() -> isize { |
| 11 | + let magic: usize = 0x10384; |
| 12 | + let mut pid: usize = 0; |
| 13 | + let mut code: i32 = 0; |
| 14 | + println!("I am the parent. Forking the child..."); |
| 15 | + pid = fork() as usize; |
| 16 | + if (pid == 0) { |
| 17 | + println!("I am the child."); |
| 18 | + yield_now(); |
| 19 | + yield_now(); |
| 20 | + yield_now(); |
| 21 | + yield_now(); |
| 22 | + yield_now(); |
| 23 | + yield_now(); |
| 24 | + yield_now(); |
| 25 | + exit(magic); |
| 26 | + } |
| 27 | + else { |
| 28 | + println!("I am parent, fork a child pid {}", pid); |
| 29 | + } |
| 30 | + if pid <= 0 { |
| 31 | + panic!("pid <= 0"); |
| 32 | + } |
| 33 | + println!("I am the parent, waiting now.."); |
| 34 | + let wait_pid = waitpid(pid, &mut code); |
| 35 | + println!("{}, {:x}", wait_pid, code); |
| 36 | + if wait_pid != 0 || code != magic as i32 { |
| 37 | + panic!("wait_test1 fail"); |
| 38 | + } |
| 39 | + if !(waitpid(pid, &mut code) != 0) { |
| 40 | + panic!("wait_test2 fail"); |
| 41 | + } |
| 42 | + println!("waitpid {} ok.", pid); |
| 43 | + println!("wait_test pass."); |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +/* |
| 48 | +out put: |
| 49 | +
|
| 50 | +I am the parent. Forking the child... |
| 51 | +I am the child. |
| 52 | +I am parent, fork a child pid 2 |
| 53 | +I am the parent, waiting now.. |
| 54 | +thread 2 exited, exit code = 66436 |
| 55 | +waitpid 2 ok. |
| 56 | +wait_test pass. |
| 57 | +thread 1 exited, exit code = 0 |
| 58 | +*/ |
0 commit comments