Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared memory vs. Go channel communication

Tags:

One of Go's slogans is Do not communicate by sharing memory; instead, share memory by communicating.

I am wondering whether Go allows two different Go-compiled binaries running on the same machine to communicate with one another (i.e. client-server), and how fast that would be in comparison to boost::interprocess in C++? All the examples I've seen so far only illustrate communication between same-program routines.

A simple Go example (with separate client and sever code) would be much appreciated!

like image 937
artur Avatar asked Nov 13 '09 17:11

artur


People also ask

Does Go support shared memory?

Go supports traditional shared memory accesses among goroutines. You can use various traditional synchronization primitives such as lock/unlock (Mutex), condition variable (Cond) and atomic read/write operations(atomic).

Do not communicate by sharing memory instead share memory by communicating meaning?

Don't communicate by sharing memory. It is like when your are communicating using threads for example you have to use variables or mutexes to lock memory for not allowing someone to read and write on it until the communication is complete. Share memory by communicating.

What are Go channels?

In Golang, or Go, channels are a means through which different goroutines communicate. Think of them as pipes through which you can connect with different concurrent goroutines. The communication is bidirectional by default, meaning that you can send and receive values from the same channel.

How would you allow communication between Goroutines in Go?

Channel in Go provides a connection between two goroutines, allowing them to communicate and synchronize the exchange of any resource that is passed through it. It is the channel's ability to control the goroutines interaction that creates the synchronization mechanism.


1 Answers

One of the first things I thought of when I read this was Stackless Python. The channels in Go remind me a lot of Stackless Python, but that's likely because (a) I've used it and (b) the language/thoughts that they actually came from I've never touched.

I've never attempted to use channels as IPC, but that's probably because the alternative is likely much safer. Here's some psuedocode:

program1

chan = channel() ipc = IPCManager(chan, None) send_to_other_app(ipc.underlying_method)  chan.send("Ahoy!") 

program2

chan = channel() recv_from_other_app(underlying_method) ipc = IPCManager(chan, underlying_method)  ahoy = chan.recv() 

If you use a traditional IPC method, you can have channels at each side that wrap their communication on top of it. This leads to some issues in implementation, which I can't even think about how to tackle, and likely a few unexpected race conditions.

However, I agree; the ability to communicate via processes using the same flexibility of Go channels would be phenomenal (but I fear unstable).

Wrapping a simple socket with channels on each side gets you almost all of the benefits, however.

like image 197
Jed Smith Avatar answered Sep 28 '22 03:09

Jed Smith