mirror of
https://github.com/rcore-os/rCore.git
synced 2024-11-22 16:16:16 +04:00
add lab1 report and test file
This commit is contained in:
parent
d1c2a2c731
commit
67fdb49ed9
BIN
labs/lab1/lab1_report_2016011330.pdf
Normal file
BIN
labs/lab1/lab1_report_2016011330.pdf
Normal file
Binary file not shown.
75
labs/lab1/sleep.rs
Normal file
75
labs/lab1/sleep.rs
Normal file
@ -0,0 +1,75 @@
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(alloc)]
|
||||
|
||||
extern crate alloc;
|
||||
#[macro_use]
|
||||
extern crate rcore_user;
|
||||
|
||||
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use core::ptr;
|
||||
|
||||
use rcore_user::io::get_line;
|
||||
|
||||
|
||||
use rcore_user::syscall::{sys_sleep, sys_vfork, sys_wait, sys_get_time, sys_exit};
|
||||
|
||||
|
||||
pub fn sleep(time: usize) -> i32 {
|
||||
sys_sleep(time)
|
||||
}
|
||||
|
||||
pub fn gettime_msec() -> u32{
|
||||
sys_get_time() as u32
|
||||
}
|
||||
|
||||
pub fn fork() -> i32 {
|
||||
sys_vfork()
|
||||
}
|
||||
|
||||
pub fn waitpid(pid: usize, code: *mut i32) -> i32 {
|
||||
sys_wait(pid, code)
|
||||
}
|
||||
|
||||
pub fn exit(error_code: usize) {
|
||||
sys_exit(error_code);
|
||||
println!("BUG: exit failed.");
|
||||
while true {};
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn sleepy(pid: usize) {
|
||||
let time: usize = 1;
|
||||
for i in 0..10 {
|
||||
sleep(time);
|
||||
println!("sleep {} x {} slices.", i + 1, time);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
// IMPORTANT: Must define main() like this
|
||||
#[no_mangle]
|
||||
pub fn main(){
|
||||
let time = gettime_msec();
|
||||
let mut pid1: usize = 0;
|
||||
let mut exit_code = 0;
|
||||
|
||||
pid1 = fork() as usize;
|
||||
|
||||
if pid1 == 0 {
|
||||
sleepy(pid1);
|
||||
} else {
|
||||
println!("child id is {}", pid1);
|
||||
}
|
||||
|
||||
assert_eq!(waitpid(pid1, &mut exit_code), pid1 as i32);
|
||||
assert_eq!(exit_code, 0);
|
||||
|
||||
println!("use {} msecs: {} to {}.", gettime_msec() - time, time, gettime_msec());
|
||||
println!("sleep pass.");
|
||||
}
|
Loading…
Reference in New Issue
Block a user