-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related: #22
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by Apache-2.0 License that can be found | ||
// in the LICENSE file. | ||
|
||
fn call_ls() { | ||
println!("[ls] pid: {}", unsafe { nc::getpid() }); | ||
let args = ["ls", "-l", "-a"]; | ||
let env = ["DISPLAY=wayland"]; | ||
let ret = unsafe { nc::execve("/bin/ls", &args, &env) }; | ||
assert!(ret.is_ok()); | ||
} | ||
|
||
fn call_date() { | ||
println!("[date] pid: {}", unsafe { nc::getpid() }); | ||
let args = ["date", "+%Y:%m:%d %H:%M:%S"]; | ||
let env = []; | ||
let ret = unsafe { nc::execve("/bin/date", &args, &env) }; | ||
assert!(ret.is_ok()); | ||
} | ||
|
||
fn main() { | ||
let pid = unsafe { nc::fork() }; | ||
match pid { | ||
Err(errno) => eprintln!("Failed to call fork(), err: {errno}"), | ||
Ok(0) => { | ||
// Child process | ||
call_ls(); | ||
} | ||
Ok(child_pid) => { | ||
// Parent process | ||
println!("[main] child pid is: {child_pid}"); | ||
} | ||
} | ||
|
||
let pid = unsafe { nc::fork() }; | ||
match pid { | ||
Err(errno) => eprintln!("Failed to call fork(), err: {errno}"), | ||
Ok(0) => { | ||
// Child process | ||
call_date(); | ||
} | ||
Ok(child_pid) => { | ||
// Parent process | ||
println!("[main] child pid is: {child_pid}"); | ||
} | ||
} | ||
|
||
// Wait for a while. | ||
let ts = nc::timespec_t { | ||
tv_sec: 2, | ||
tv_nsec: 0, | ||
}; | ||
unsafe { | ||
let _ret = nc::nanosleep(&ts, None); | ||
} | ||
} |