1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
use libc::{getpid, getppid}; #[derive(Debug, Clone)] pub enum ForkPid { Parent((i32, i32)), Children((i32, i32)), None, } #[derive(Debug, Default, Clone)] pub struct Fork; impl Fork { pub fn fork() -> ForkPid { let pid = unsafe { libc::fork() }; match pid { 0 => ForkPid::Children((unsafe { getppid() }, unsafe { getpid() })), 1..=std::i32::MAX => ForkPid::Parent((unsafe { getpid() }, pid)), _ => ForkPid::None, } } } #[cfg(test)] mod fork { #[test] fn fork_box() {} }