Two application sharing the same stream

This is probably a question less about Rust and more about process communication. Even a point to the right wikipedia article would be helpful.

I have a multi threaded rust application that I finally got working (thanks to the help from the forum), and I am understanding what’s happening (I wrote a stdlib version, a crossbeam version, a version that used channels, and a version that shared state via Mutex). The Book was very helpful.

My question is “how can I extend the sharing of state beyond threads and into processes.”

More specifically, I want to have a rust application (A) that streams binary data to another application (B) on the same machine. I know that I can use a Tcp or Udp socket and have application B constantly query application A, but it seems like there would be a simpler way to write to a shared buffer or something.

Both applications are rust, though Application B is actually a Rust library that exposes a C api and is imported as a library into a running C application. I can have running threads in that application to process the data.

The ultimate goal, really, is to have the fastest and lowest cpu intensive method of sending simple binary data from one running application to another. Message ordering isn’t super important, though retaining mostly in order would be appreciated.

I would really appreciate a step in the right direction.

You have a couple of different options, depending on the OS you are targeting.

Linux offers IPC using message passing with UDS (similar to TCP/UDP over the local interface), IPC using shared memory shm/mmap with a futex or tmpfs which provides a filesystem abstraction.

macOS is similar to Linux; it supports UDS (with some small differences), shm, and IPC sync primitives. Instead of tmpfs, a RAM disk can be mounted.

Windows offers named pipes which are similar to unix domain sockets, and named shared memory and file mapping which are similar to mmap/tmpfs. Access can be synchronized with IPC sync primitives There’s also MS-MPI but I really don’t know what all that entails.

And as for Rust … the nix, mach, and winapi crates can help you access the APIs mentioned above.

Wow @parasyte, this is exactly the answer I was looking for. UDS is perfect for my needs, and using mio seems to be the way to go. I’m going to play around with mio and tokio, but I’d also like to at least understand how to do the basics in vanilla rust with the stdlib.

Thanks for the quick and comprehensive answer :slight_smile: