Skip to content

Instantly share code, notes, and snippets.

@garypen
Created August 19, 2019 14:11
Show Gist options
  • Save garypen/5b7a97f26e148dfe5397c54466bb7011 to your computer and use it in GitHub Desktop.
Save garypen/5b7a97f26e148dfe5397c54466bb7011 to your computer and use it in GitHub Desktop.
rust fork example
[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"
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