From 9df03206f972391f56e0cb6df185d6a251658300 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Sun, 23 Jan 2022 01:39:09 -0800 Subject: [PATCH] Add infloop&until_timeout --- user/src/bin/infloop.rs | 9 +++++++ user/src/bin/until_timeout.rs | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 user/src/bin/infloop.rs create mode 100644 user/src/bin/until_timeout.rs diff --git a/user/src/bin/infloop.rs b/user/src/bin/infloop.rs new file mode 100644 index 00000000..6e8964d5 --- /dev/null +++ b/user/src/bin/infloop.rs @@ -0,0 +1,9 @@ +#![no_std] +#![no_main] + +extern crate user_lib; + +#[no_mangle] +pub fn main(_argc: usize, _argv: &[&str]) -> ! { + loop {} +} diff --git a/user/src/bin/until_timeout.rs b/user/src/bin/until_timeout.rs new file mode 100644 index 00000000..2a5c0529 --- /dev/null +++ b/user/src/bin/until_timeout.rs @@ -0,0 +1,44 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use user_lib::{fork, exec, kill, waitpid_nb, waitpid, get_time, SignalFlags}; + +#[no_mangle] +pub fn main(argc: usize, argv: &[&str]) -> i32 { + assert_eq!(argc, 3, "argc must be 3!"); + let timeout_ms = argv[2].parse::().expect("Error when parsing timeout!"); + let pid = fork() as usize; + if pid == 0 { + if exec(argv[1], &[core::ptr::null::()]) != 0 { + println!("Error when executing '{}'", argv[1]); + return -4; + } + } else { + let start_time = get_time(); + let mut child_exited = false; + let mut exit_code: i32 = 0; + loop { + if get_time() - start_time > timeout_ms { + break; + } + if waitpid_nb(pid, &mut exit_code) as usize == pid { + child_exited = true; + println!( + "child exited in {}ms, exit_code = {}", + get_time() - start_time, + exit_code, + ); + } + } + if !child_exited { + println!("child has run for {}ms, kill it!", timeout_ms); + kill(pid, SignalFlags::SIGINT.bits()); + assert_eq!(waitpid(pid, &mut exit_code) as usize, pid); + println!("exit code of the child is {}", exit_code); + } + } + 0 +}