use crate::error::Result;
use anyhow::anyhow;
#[cfg(feature = "process")]
use sysinfo::{Pid, System};
pub fn get_current_pid() -> u32 {
std::process::id()
}
#[cfg(feature = "process")]
pub fn is_process_running(pid: u32) -> bool {
let mut sys = System::new_all();
sys.refresh_all();
sys.process(Pid::from(pid as usize)).is_some()
}
#[cfg(feature = "process")]
pub fn kill_process(pid: u32) -> Result<()> {
let mut sys = System::new_all();
sys.refresh_all();
if let Some(process) = sys.process(Pid::from(pid as usize)) {
if process.kill() {
Ok(())
} else {
Err(anyhow!("无法终止进程 {}", pid))
}
} else {
Err(anyhow!("找不到进程 {}", pid))
}
}