Pidfd
Rust crate providing Linux (>= 5.3) PID file descriptor support
Install / Use
/learn @pop-os/PidfdREADME
pidfd
This Rust crate provides Linux (>= 5.3) PID file descriptor support. PID file descriptors are created from PIDs of processes, and are guaranteed to always reference the process from which the PID FD was created.
One of the benefits of using a PID FD is the ability to use poll(), select(), and epoll() to monitor when the process has terminated. This makes it ideal for use in asynchronous programming. This crate implements std::future::Future on the PidFd type so that multiple processes can be awaited concurrently.
Linux 5.4 is required to use the
waitidfeature, which enables fetching the exit status of a pidfd.
use pidfd::PidFd;
use std::{io, process::Command};
fn main() {
futures::executor::block_on(async move {
futures::try_join!(
spawn_sleeper("1", "5"),
spawn_sleeper("2", "4"),
spawn_sleeper("3", "3"),
spawn_sleeper("4", "2"),
spawn_sleeper("5", "1"),
)
.unwrap();
})
}
async fn spawn_sleeper(id: &str, timeout: &str) -> io::Result<()> {
println!("started job {}", id);
let exit_status = Command::new("/bin/sleep")
.arg(timeout)
.spawn()
.map(PidFd::from)
.unwrap()
.into_future()
.await?;
println!("finished job {}: {}", id, exit_status);
Ok(())
}
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Related Skills
openhue
337.4kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
337.4kElevenLabs text-to-speech with mac-style say UX.
weather
337.4kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.4kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
