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

标签 process rust

我有一个在 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/

相关文章:

c++ - Qt5 : Preventing another instance of the application doesn't work anymore. ..!

rust - 如何配置 Geany 为 Rust 完成代码?

php - 如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?

asynchronous - 如何限制 async_trait 函数的返回值来实现同步?

mongodb - 使用 mongodb-1.2.2 和 Rocket-0.5.0-rc.1 时如何解决异步不兼容问题?

pointers - Rust,Copy Trait 在使用泛型时不适用于类型

rust - 如何将拥有的盒装结构引用给其他拥有的结构

Node.js 拦截 process.exit

flockfile函数可以用于进程吗

linux - 如何在 Bash 中将进程的子进程输出为数字