ipc_write/
ipc_write.rs

1//! Example of inter-process communication
2//!
3//! Start this example first, then `ipc_read`.
4
5use std::ffi::CString;
6use std::io::{Read, Write};
7
8#[cfg(target_os = "linux")]
9type Mode = libc::mode_t;
10
11#[cfg(target_os = "macos")]
12type Mode = libc::c_uint;
13
14fn main() -> std::io::Result<()> {
15    let path = CString::new("cueue_ipc").unwrap();
16    let mode = (libc::S_IRUSR | libc::S_IWUSR) as Mode;
17    let f = unsafe { libc::shm_open(path.as_ptr(), libc::O_RDWR | libc::O_CREAT, mode) };
18    if f < 0 {
19        return Err(std::io::Error::last_os_error());
20    }
21
22    let (mut w, _) = cueue::cueue_in_fd(f, Some(1 << 20))?;
23
24    loop {
25        print!("> ");
26        std::io::stdout().flush()?;
27        let buf = w.write_chunk();
28        match std::io::stdin().read(buf) {
29            Ok(0) | Err(_) => break,
30            Ok(n) => {
31                w.commit(n);
32            }
33        }
34    }
35
36    unsafe {
37        libc::shm_unlink(path.as_ptr());
38    }
39
40    Ok(())
41}