Lorenz Bung 8 lat temu
rodzic
commit
d6310fb63a
1 zmienionych plików z 15 dodań i 4 usunięć
  1. 15
    4
      hw6/task1/src/main.rs

+ 15
- 4
hw6/task1/src/main.rs Wyświetl plik

3
 
3
 
4
 extern crate nix;
4
 extern crate nix;
5
 
5
 
6
-use nix::unistd::{fork, getpid};
6
+use nix::unistd::{fork, getpid, pipe, read, write};
7
 use nix::sys::wait::wait;
7
 use nix::sys::wait::wait;
8
+use std::str;
8
 use nix::sys::wait::WaitStatus;
9
 use nix::sys::wait::WaitStatus;
9
 use nix::unistd::ForkResult::{Child, Parent};
10
 use nix::unistd::ForkResult::{Child, Parent};
10
 
11
 
14
 
15
 
15
     if arguments.len() > 2 {
16
     if arguments.len() > 2 {
16
 
17
 
17
-
18
+        let (reader, writer) = pipe().unwrap();
19
+        let msg = "hello from parent";
18
         let pid = fork();
20
         let pid = fork();
19
         match pid {
21
         match pid {
20
             Ok(Child) => {
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
             Ok(Parent { child }) => {
29
             Ok(Parent { child }) => {
30
+
31
+                println!("sending to child with pid:{}", child);
32
+                write(writer, msg.as_bytes()).unwrap();
33
+
25
                 if let Ok(ws) = wait() {
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
 

Ładowanie…
Anuluj
Zapisz