Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement inter-process communication in Go?

Tags:

go

ipc

People also ask

Which method is used for inter process communication?

Different ways of IPC are pipe, message passing, message queue, shared memory, direct communication, indirect communication and FIFO. It is important to obtain synchronization among processes in IPC to maintain data consistency. Semaphore and mutex are two ways to do so.

How do you communicate between two processes?

Two-way communication between processes can be achieved by using two pipes in opposite "directions". A pipe that is treated like a file. Instead of using standard input and output as with an anonymous pipe, processes write to and read from a named pipe, as if it were a regular file.

How is Inter process communication implemented through shared memory?

Inter Process Communication through shared memory is a concept where two or more process can access the common memory. And communication is done via this shared memory where changes made by one process can be viewed by another process.


Go has a built-in RPC system (http://golang.org/pkg/rpc/) for easy communication between Go processes.

Another option is to send gob-encoded data (http://blog.golang.org/2011/03/gobs-of-data.html) via network connection.

You shouldn't dismiss local networking without benchmarking. For example Chrome uses named pipes for IPC and they transfer a lot of data (e.g. rendered bitmaps) between processes:

Our main inter-process communication primitive is the named pipe. On Linux & OS X, we use a socketpair()

-- http://www.chromium.org/developers/design-documents/inter-process-communication

If named pipes are good enough for that, they are probably good enough for your use case. Plus, if you write things well, you could start using named pipes (because it's easy) and then switch to shared memory if you find performance of named pipes not good enough (shared memory is not easy regardless of the language).


I'd suggest looking at 0mq. It's a messaging library designed to be be fast and easy whether you use it over the network, for local IPC, or even inter-thread communication. It handles a lot of the tricky bits of IPC, like making senders back off sending requests if the receiver is getting overloaded, message framing, and reconnecting after failure. And it has bindings for LOTS of languages, including Go, which makes it useful for wiring together systems written in different languages.


Go's builtin RPC package is still usable, but note that it's frozen due to outstanding bugs that are hard to fix (see https://github.com/golang/go/issues/16844 for details).