當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Rust PidFd用法及代碼示例

本文簡要介紹rust語言中 Struct std::os::linux::process::PidFd 的用法。

用法

pub struct PidFd { /* fields omitted */ }

此類型表示引用進程的文件說明符。

PidFd 可以通過在 Command 上使用 create_pidfd 設置相應的選項來獲得。隨後,可以通過調用 pidfd take_pidfd Child 中檢索創建的 pidfd。

例子:

#![feature(linux_pidfd)]
use std::os::linux::process::{CommandExt, ChildExt};
use std::process::Command;

let mut child = Command::new("echo")
    .create_pidfd(true)
    .spawn()
    .expect("Failed to spawn child");

let pidfd = child
    .take_pidfd()
    .expect("Failed to retrieve pidfd");

// The file descriptor will be closed when `pidfd` is dropped.

有關詳細信息,請參閱 pidfd_open(2) 的手冊頁。

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Struct std::os::linux::process::PidFd。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。