如何向“Child"子进程发送信号? [英] How do I send a signal to a `Child` subprocess?
本文介绍了如何向“Child"子进程发送信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Child::kill
发送一个 SIGKILL
,但我如何发送任何其他信号,例如 SIGTERM
?我可能可以使用 libc
及其信号 API,但有没有更好的方法来做到这一点?
Child::kill
sends a SIGKILL
, but how can I send any other signal such as SIGTERM
? I can probably use libc
and its signal API, but is there a better way to do this?
推荐答案
nix
库在为低级 UNIX 操作(包括发送和处理信号)提供惯用的 Rust 包装方面做得很好.在这种情况下,您将创建一个 nix::Pid
来自 child_process.id()
,然后将其传递给 kill
像这样:
The nix
library does a good job of providing idiomatic rust wrappers around low-level UNIX operations, including sending and handling signals. In this case, you would create a nix::Pid
from child_process.id()
, then pass it to kill
like so:
use nix::unistd::Pid;
use nix::sys::signal::{self, Signal};
// Spawn child process.
let mut child = std::process::Command::new();
/* build rest of command */
child.spawn().unwrap();
// Send SIGTERM to child process.
signal::kill(Pid::from_raw(child.id()), Signal::SIGTERM).unwrap();
这篇关于如何向“Child"子进程发送信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文