Send messages between two child process rust nix

Issue

I am trying to implement a exercise with child process and pipes in rust. The exercise consist in child process 2 send message to child process 1, the message that I recieved in c1 I have to return to c2, I have to do it 10 times every 2 seconds. I have a code that might to do it, but when I want to run it, it’s still waiting and I can,t see the messages.

This is my code:

use nix::sys::wait::wait;
use nix::unistd::ForkResult::{Child, Parent};
use nix::unistd::fork;
use std::io::prelude::*;
use std::time::Duration;

fn main() {
    let (mut reader, mut writer) = os_pipe::pipe().unwrap();
    let (mut reader_2, mut writer_2) = os_pipe::pipe().unwrap();
    let pid = fork();

    match pid.expect("Error during creating child") {
        Parent { child } => {
            drop(reader);
            drop(writer_2);
            let pid_2 = fork();
            match pid_2.expect("Error creating child 2") {
                Parent { child } => {}
                Child => {
                    for _ in 1..10 {
                        writer.write("C1".as_bytes()).unwrap();
                        let mut data = String::new();
                        reader_2.read_to_string(&mut data).unwrap();
                        println!("Data from C1, {}", data);
                        std::thread::sleep(Duration::from_millis(2000));
                    }
                }
            }
        }

        Child => {
            drop(writer);
            drop(reader_2);
            for _ in 1..10 {
                let mut data = String::new();
                reader.read_to_string(&mut data).unwrap();
                println!("Data from C2, {}", data);
                writer_2.write(data.to_string().as_bytes()).unwrap();
            }
        }
    }
    wait().unwrap();
}

Thanks!!!

Solution

You can change the data to a u8 slice of size 2 and use read_exact instead of read_to_string:

        Parent { child } => {
            drop(reader);
            drop(writer_2);
            let pid_2 = unsafe{ fork() };
            match pid_2.expect("Error creating child 2") {
                Parent { child } => {}
                Child => {
                    let mut data: [u8;2] = [0;2];
                    for _ in 1..10 {
                        writer.write("C1".as_bytes()).unwrap();
                        reader_2.read_exact(&mut data).unwrap();
                        println!("Data from C1, {}", std::str::from_utf8(&data).unwrap());
                        std::thread::sleep(Duration::from_millis(2000));
                    }
                }
            }
        }

        Child => {
            drop(writer);
            drop(reader_2);
            let mut data: [u8;2] = [0;2];
            for _ in 1..10 {
                reader.read_exact(&mut data).unwrap();
                println!("Data from C2, {}", std::str::from_utf8(&data).unwrap());
                writer_2.write(&data).unwrap();
            }
        }
    }

Answered By – fred xia

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published