Function nix::unistd::fork [] [src]

pub fn fork() -> Result<ForkResult>

Create a new child process duplicating the parent process (see fork(2)).

After calling the fork system call (successfully) two processes will be created that are identical with the exception of their pid and the return value of this function. As an example:

use nix::unistd::{fork, ForkResult};

match fork() {
   Ok(ForkResult::Parent { child, .. }) => {
       println!("Continuing execution in parent process, new child has pid: {}", child);
   }
   Ok(ForkResult::Child) => println!("I'm a new child process"),
   Err(e) => println!("Fork failed"),
}

This will print something like the following (order indeterministic). The thing to note is that you end up with two processes continuing execution immediately after the fork call but with different match arms.

Continuing execution in parent process, new child has pid: 1234
I'm a new child process