Spawning processes with Rust
In the Rust standard library, std::process
is the module for working with processes. In this section, we'll look at how to spawn new processes, interact with child processes, and abort the current process using the Rust standard library. The Rust standard library internally uses the corresponding Unix/Linux syscalls to invoke the kernel operations for managing processes.
Let's begin with launching new child processes.
Spawning new child processes
The std::process::Command
is used to launch a program at a specified path, or to run a standard shell command. The configuration parameters for the new process can be constructed using a builder pattern. Let's see a simple example:
use std::process::Command; fn main() { Command::new("ls") .spawn() .expect("ls command failed to start"); }
The code...