|
|
@@ -3,8 +3,9 @@ use std::process;
|
|
3
|
3
|
|
|
4
|
4
|
extern crate nix;
|
|
5
|
5
|
|
|
6
|
|
-use nix::unistd::{fork, getpid};
|
|
|
6
|
+use nix::unistd::{fork, getpid, pipe, read, write};
|
|
7
|
7
|
use nix::sys::wait::wait;
|
|
|
8
|
+use std::str;
|
|
8
|
9
|
use nix::sys::wait::WaitStatus;
|
|
9
|
10
|
use nix::unistd::ForkResult::{Child, Parent};
|
|
10
|
11
|
|
|
|
@@ -14,16 +15,26 @@ fn main() {
|
|
14
|
15
|
|
|
15
|
16
|
if arguments.len() > 2 {
|
|
16
|
17
|
|
|
17
|
|
-
|
|
|
18
|
+ let (reader, writer) = pipe().unwrap();
|
|
|
19
|
+ let msg = "hello from parent";
|
|
18
|
20
|
let pid = fork();
|
|
19
|
21
|
match pid {
|
|
20
|
22
|
Ok(Child) => {
|
|
21
|
|
- println!("hello, I am child (pid:{})", getpid());
|
|
|
23
|
+ let mut read_buf = [0u8; 32];
|
|
|
24
|
+ let bytes_read = read(reader, &mut read_buf).unwrap();
|
|
|
25
|
+ let msg_received = str::from_utf8(&read_buf[0..bytes_read]).unwrap();
|
|
|
26
|
+ println!("{} received from parent:{:?}", getpid(), msg_received);
|
|
22
|
27
|
}
|
|
23
|
28
|
|
|
24
|
29
|
Ok(Parent { child }) => {
|
|
|
30
|
+
|
|
|
31
|
+ println!("sending to child with pid:{}", child);
|
|
|
32
|
+ write(writer, msg.as_bytes()).unwrap();
|
|
|
33
|
+
|
|
25
|
34
|
if let Ok(ws) = wait() {
|
|
26
|
|
- if let WaitStatus::Exited(child_pid, exit_code) = ws {}
|
|
|
35
|
+ if let WaitStatus::Exited(child_pid, exit_code) = ws {
|
|
|
36
|
+ println!("Parent exits");
|
|
|
37
|
+ }
|
|
27
|
38
|
}
|
|
28
|
39
|
}
|
|
29
|
40
|
|