waitpid

Function waitpid 

Source
pub fn waitpid(pid: pid_t) -> Result<c_int>
Expand description

Wait for process to change status see wait(2)

§Behavior

  • Retries automatically on EINTR (interrupted by signal)
  • Returns the raw status (use libc::WIFEXITED, libc::WEXITSTATUS, etc.)

§Errors

Returns an io::Error if the waitpid system call fails. Common errors include:

  • No child process exists with the given PID
  • Invalid options or PID

Example:

use fork::{waitpid, Fork};

fn main() {
 match fork::fork() {
    Ok(Fork::Parent(pid)) => {
        println!("Child pid: {pid}");
        match waitpid(pid) {
            Ok(status) => println!("Child exited with status: {status}"),
            Err(e) => eprintln!("Failed to wait on child: {e}"),
        }
    }
    Ok(Fork::Child) => {
        // Child does trivial work then exits
        std::process::exit(0);
    }
    Err(e) => eprintln!("Failed to fork: {e}"),
 }
}