Created
August 19, 2019 14:11
-
-
Save garypen/5b7a97f26e148dfe5397c54466bb7011 to your computer and use it in GitHub Desktop.
rust fork example
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
[package] | |
name = "forker" | |
version = "0.1.0" | |
authors = ["author <author email>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
nix = "0.15" |
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
use nix::unistd::{fork, ForkResult}; | |
use std::process::id; | |
fn main() { | |
match fork() { | |
Ok(ForkResult::Parent { child, .. }) => { | |
println!( | |
"This is the original process(pid: {}). New child has pid: {}", | |
id(), | |
child | |
); | |
} | |
Ok(ForkResult::Child) => println!("This is the new process(pid: {}).", id()), | |
Err(_) => println!("Something went wrong."), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment