Fifo Rust compile and the process dont sleep but doesnt work how i want

This code is ok because the exec its working but is not working how i want. when i write the variable contents in the terminal it doesnt have anything(sorry for my english)

use nix::unistd::{fork};
use nix::unistd::ForkResult::{Child, Parent};
use std::fs::File;
use std::io::{Read, Write};
use std::io::{BufReader, BufWriter};
use nix::sys::wait::waitpid;
use std::ffi::CString;
use std::fs;


fn main(){
    let fifo_path = "/tmp/fifojuan";
    let filename = CString::new(fifo_path).unwrap();
    unsafe {
        libc::mkfifo(filename.as_ptr(), 0o644);
    }

    match unsafe { fork() } {
        Ok(Child) => {
            let new_name = CString::new("Cli2").unwrap();
            unsafe {
                libc::prctl(libc::PR_SET_NAME, new_name.as_ptr(), 0, 0, 0);
            }
            let fifo_write = File::create(fifo_path).unwrap();
            let mut writer = BufWriter::new(&fifo_write);

            let mensaje = "Hola desde el proceso hijo!";
            writer.write_all(mensaje.as_bytes()).unwrap();


            drop(writer);

            std::process::exit(0);

        }
        Ok(Parent { child}) => {
            let new_name = CString::new("Serv2").unwrap();
            unsafe {
                libc::prctl(libc::PR_SET_NAME, new_name.as_ptr(), 0, 0, 0);
            }
            let fifo_read = File::open(fifo_path).unwrap();
            let mut reader = BufReader::new(&fifo_read);


            let mut data = String::new();

            match reader.read_to_string(&mut data) {
                Ok(_) => {
                    let mut file = File::open(data.trim()).expect("No se pudo abrir el archivo");
                    let mut contents = String::new();
                    file.read_to_string(&mut contents).expect("No se pudo leer el archivo");
                    drop(reader);
                    println!("{}", contents);
        
                    match waitpid(child, None) {
                        Ok(_) => {
                            match fs::remove_file(fifo_path) {
                                Ok(()) => println!("El FIFO ha sido eliminado exitosamente."),
                                Err(e) => println!("Error al eliminar el FIFO: {}", e),
                            }
                        }
                        Err(e) => {
                            println!("Error en waitpid(): {}", e);
                        }
                    }
                    
                }
                Err(_) => {
                    println!("Reader closed");
                }
            }
        }
        Err(_) => {
            println!("Fork failed");
        }
    }
}

~/work2/rust_fifo$ cargo run
Compiling rust_fifo v0.1.0 (/home/juang/work2/rust_fifo)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running target/debug/rust_fifo

El FIFO ha sido eliminado exitosamente.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.