MCPcopy Create free account
hub / github.com/chyyuu/os_kernel_lab / main

Function main

user/src/bin/until_timeout.rs:10–46  ·  view source on GitHub ↗
(argc: usize, argv: &[&str])

Source from the content-addressed store, hash-verified

8
9#[no_mangle]
10pub fn main(argc: usize, argv: &[&str]) -> i32 {
11 assert_eq!(argc, 3, "argc must be 3!");
12 let timeout_ms = argv[2]
13 .parse::<isize>()
14 .expect("Error when parsing timeout!");
15 let pid = fork() as usize;
16 if pid == 0 {
17 if exec(argv[1], &[core::ptr::null::<u8>()]) != 0 {
18 println!("Error when executing '{}'", argv[1]);
19 return -4;
20 }
21 } else {
22 let start_time = get_time();
23 let mut child_exited = false;
24 let mut exit_code: i32 = 0;
25 loop {
26 if get_time() - start_time > timeout_ms {
27 break;
28 }
29 if waitpid_nb(pid, &mut exit_code) as usize == pid {
30 child_exited = true;
31 println!(
32 "child exited in {}ms, exit_code = {}",
33 get_time() - start_time,
34 exit_code,
35 );
36 }
37 }
38 if !child_exited {
39 println!("child has run for {}ms, kill it!", timeout_ms);
40 kill(pid, SignalFlags::SIGINT.bits());
41 assert_eq!(waitpid(pid, &mut exit_code) as usize, pid);
42 println!("exit code of the child is {}", exit_code);
43 }
44 }
45 0
46}

Callers

nothing calls this directly

Calls 5

forkFunction · 0.85
execFunction · 0.85
waitpid_nbFunction · 0.85
killFunction · 0.85
get_timeFunction · 0.50

Tested by

no test coverage detected