我正在try 使用fork()创建子进程并通过管道进行通信.我使用的是nix库和os_pive.当我try 执行该程序时,它正在等待,并且我看不到我在子进程中向父进程发送的消息.我试着用Drop功能来关闭管道,但没有起作用.这是我的代码:
use nix::sys::wait::wait;
use nix::unistd::ForkResult::{Child, Parent};
use nix::unistd::{fork, getpid, getppid, pipe};
use std::io::prelude::*;
fn main() {
let pid = fork();
let (mut reader, mut writer) = os_pipe::pipe().unwrap();
match pid.expect("Error during creating child") {
Parent { child } => {
println!("From parent, {}", getpid());
wait().unwrap();
let mut data = String::new();
reader.read_to_string(&mut data).unwrap();
println!("data: {}", data);
}
Child => {
println!("from child, {}", getpid());
writer.write_all("hello".as_bytes()).unwrap();
}
}
}
谢谢!