Understanding nix fork concurrency
Before threads were introduced as a standard for POSIX operating systems in 1995, the best option available for concurrency was fork
. On these operating systems, fork
was a fairly primitive command that allowed programs to create copies of themselves as child processes. The name fork
comes from the idea of taking one process and splitting it into two.
fork
is not platform-independent, specifically it is not available on Windows, and we recommend using threads instead. However, for educational purposes, it is helpful to introduce some of the concepts from fork
because they are also relevant to threaded programming.
The following code is a translation of the preceding process_a
, process_b
example to use fork
:
extern crate nix; use nix::unistd::{fork,ForkResult}; use std::{thread,time}; use std::process; fn main() { let mut children = Vec::new(); for _ in 0..3 { match fork().expect("fork failed") { ForkResult::Parent{ child: pid } =>...