gpt4 book ai didi

process - 如何在 Rust 中终止 Windows 上正在运行的子进程?

转载 作者:行者123 更新时间:2023-11-29 08:03:35 30 4
gpt4 key购买 nike

我有一个在 Windows 上创建进程的函数。

pub fn create_process(url: String) {
thread::spawn(move || {
let _child = process::Command::new("cmd.exe")
.arg("/C")
.arg("ping")
.arg(&url)
.arg("-t")
.spawn()
.expect("Couldn't run 'ping'");
});
}

我有一个函数,我想终止(杀死)由“create_process()”创建的进程:

pub fn stop() {
// ?????
}

如何访问在 create_process 函数中创建的 _child 以终止其进程?还有其他方法可以终止该进程吗?

最佳答案

长话短说:使用 kill方法。例如:

use std::{process, thread, time::Duration};

fn main() {
let mut child = process::Command::new("ping")
.arg("8.8.8.8")
.arg("-t")
.spawn()
.expect("Couldn't run 'ping'");

thread::sleep(Duration::from_secs(5));
child.kill().expect("!kill");
}

请注意您不需要单独的线程,因为派生进程已经与父进程并行。

在您的问题中,有一个代码示例使用“cmd/C”来运行“ping”。这产生的不是一个而是两个进程:“cmd”进程和“ping”进程。杀死 child 将杀死“cmd”进程,但可能会使“ping”进程继续运行。

使用“cmd/C”也很危险,考虑到 command injection .

How can I access the _child created in the create_process function to terminate its process?

_child is Send ,这意味着您可以将它从一个线程发送到另一个线程。许多相应的 Stack Overflow 问题可能已经涵盖了跨线程发送数据的细节。

Are there any other ways to kill that process?

您可以使用 native 平台 API。例如:

[dependencies]
gstuff = "0.5.2"
winapi = {version = "0.3.6", features = ["psapi", "shellapi"]}
#[macro_use]
extern crate gstuff;

use std::process;
use std::ptr::null_mut;
use std::thread;
use std::time::Duration;
use winapi::shared::minwindef::DWORD;
use winapi::shared::ntdef::HANDLE;
use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess};
use winapi::um::winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE};

struct Process(HANDLE);
impl Process {
fn open(pid: DWORD) -> Result<Process, String> {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684320%28v=vs.85%29.aspx
let pc = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, 0, pid) };
if pc == null_mut() {
return ERR!("!OpenProcess");
}
Ok(Process(pc))
}

fn kill(self) -> Result<(), String> {
unsafe { TerminateProcess(self.0, 1) };
Ok(())
}
}
impl Drop for Process {
fn drop(&mut self) {
unsafe { winapi::um::handleapi::CloseHandle(self.0) };
}
}

fn main() {
let child = process::Command::new("ping")
.arg("8.8.8.8")
.arg("-t")
.spawn()
.expect("Couldn't run 'ping'");

let pid = child.id();
let pc = Process::open(pid as DWORD).expect("!open");
println!("Process {} opened.", pid);
thread::sleep(Duration::from_secs(5));
pc.kill().expect("!kill");
println!("Process {} killed.", pid);
}

关于process - 如何在 Rust 中终止 Windows 上正在运行的子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55230450/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com