Skip to content

Commit e3410da

Browse files
author
刘丰源
committed
add fork test & wait test
1 parent a68e030 commit e3410da

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

test/usr/fork_test.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
#[macro_use]
5+
extern crate user;
6+
7+
use user::syscall::sys_fork;
8+
9+
#[no_mangle]
10+
pub fn main() -> usize {
11+
let tid = sys_fork();
12+
let tid = sys_fork();
13+
if tid == 0 {
14+
println!("I am child");
15+
} else {
16+
println!("I am father");
17+
}
18+
println!("ret tid is: {}", tid);
19+
0
20+
}
21+
22+
/*
23+
out put:
24+
25+
I am child
26+
ret tid is: 0
27+
thread 3 exited, exit code = 0
28+
I am father
29+
ret tid is: 3
30+
thread 2 exited, exit code = 0
31+
I am child
32+
ret tid is: 0
33+
thread 4 exited, exit code = 0
34+
I am father
35+
ret tid is: 4
36+
thread 1 exited, exit code = 0
37+
*/

test/usr/wait_test.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)