pub fn wait_any() -> Result<(pid_t, c_int)>Expand description
Wait for any child process to terminate see wait(2)
This is equivalent to waitpid(-1, ...): it blocks until any child process
terminates, then returns both the PID that was reaped and its raw status.
Use this when supervising multiple children. Unlike waitpid(), which is
aimed at a specific child PID and returns only the status, this function
preserves the child PID returned by the underlying waitpid system call.
§Reaping behavior
Because this calls waitpid(-1, ...), it reaps any child of the current
process — including children spawned by other parts of the program, such as
std::process::Command. If you reap such a child here, the standard
library’s std::process::Child::wait/try_wait will later fail with
ECHILD because the child no longer exists. Only use wait_any() in
programs that manage all of their children directly through this crate.
§Behavior
- Blocks until any child terminates
- Retries automatically on
EINTR(interrupted by signal) - Returns
(pid, status)wherepididentifies the child that was reaped - 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 unwaited-for child processes exist (ECHILD)
- Invalid options
§Example
use fork::{fork, wait_any, Fork, WEXITSTATUS, WIFEXITED};
match fork::fork() {
Ok(Fork::Parent(child)) => {
let (pid, status) = wait_any()?;
assert_eq!(pid, child);
assert!(WIFEXITED(status));
assert_eq!(WEXITSTATUS(status), 0);
}
Ok(Fork::Child) => std::process::exit(0),
Err(e) => eprintln!("Fork failed: {e}"),
}