fork/
fork.rs

1// Copyright (c) 2024 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
2// Use of this source is governed by Apache-2.0 License that can be found
3// in the LICENSE file.
4
5fn main() {
6    let pid = unsafe { nc::fork() };
7    match pid {
8        Err(errno) => eprintln!("Failed to call fork(), err: {}", nc::strerror(errno)),
9        Ok(0) => {
10            // Child process
11            println!("[child] pid: {}", unsafe { nc::getpid() });
12            let args = ["ls", "-l", "-a"];
13            let env = ["DISPLAY=wayland"];
14            let ret = unsafe { nc::execve("/bin/ls", &args, &env) };
15            assert!(ret.is_ok());
16        }
17        Ok(child_pid) => {
18            // Parent process
19            println!("[main] child pid is: {child_pid}");
20        }
21    }
22}