Vitus 5db1f9ad54
refactor(ipc): Refactor the syscalls in ipc (#1183)
* feat(ipc): 完成对ipc的系统调用的重构

* refactor(ipc): 优化结构

Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com>

* feat: fmt

Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com>

---------

Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com>
Co-authored-by: Vitus <zhzvitus@gmail.com>
Co-authored-by: sparkzky <sparkhhhhhhhhhh@outlook.com>
2025-05-30 20:52:44 +08:00

45 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::arch::ipc::signal::{SigCode, Signal};
use crate::ipc::signal_types::{SigInfo, SigType};
use crate::process::{process_group::Pgid, Pid, ProcessManager};
use core::sync::atomic::compiler_fence;
use system_error::SystemError;
/// ### 杀死一个进程
pub fn kill_process(pid: Pid, sig: Signal) -> Result<usize, SystemError> {
// 初始化signal info
let mut info = SigInfo::new(sig, 0, SigCode::User, SigType::Kill(pid));
compiler_fence(core::sync::atomic::Ordering::SeqCst);
let ret = sig
.send_signal_info(Some(&mut info), pid)
.map(|x| x as usize);
compiler_fence(core::sync::atomic::Ordering::SeqCst);
ret
}
/// ### 杀死一个进程组
pub fn kill_process_group(pgid: Pgid, sig: Signal) -> Result<usize, SystemError> {
let pg = ProcessManager::find_process_group(pgid).ok_or(SystemError::ESRCH)?;
let inner = pg.process_group_inner.lock();
for pcb in inner.processes.values() {
kill_process(pcb.pid(), sig)?; // Call the new common function
}
Ok(0)
}
/// ### 杀死所有进程
/// - 该函数会杀死所有进程除了当前进程和init进程
pub fn kill_all(sig: Signal) -> Result<usize, SystemError> {
let current_pid = ProcessManager::current_pcb().pid();
let all_processes = ProcessManager::get_all_processes();
for pid_val in all_processes {
if pid_val == current_pid || pid_val.data() == 1 {
continue;
}
kill_process(pid_val, sig)?; // Call the new common function
}
Ok(0)
}