[zeromq-dev] Shared memory transport (IPC)
sustrik at 250bpm.com
Sun Nov 15 12:15:49 CET 2009
As discussed beforehand off-list, here's my idea how shared memory
transport can be implemented:
- first of all the two processes have to have a notification mechanism
between them; let's say we'll use a TCP connection for this (in the
future we may experiment with alternatives such as pipe or eventfd); to
implement transport that has speaks TCP to another process, simply copy
existing 0mq TCP "engine" (bp_tcp_engine_t) and name it something
different (shmem_engine_t or so); you'll also have to modify
engine_factory.cpp so that when a specific prefix is used ("shmem")
shmem_engine_t is created; thus, the address parameter to bind/connect
functions would like something like this: "shmem://127.0.0.1:5555"
- as shared memory won't work accross the machines anyway, we can assume
the interface to use is always loopback (127.0.0.1) and simplify the
address to "shmem://5555"; additionally, we can think about how to
transfer port number via shared memory and thus get rid of TCP settings
- add name of the file associated with shared memory to create to the
address, say: "shmem://home/sustrik/myfile"
- on the "bind" side of the transport, the shared memory identified by
the filename should be created, on the "connect" side you should bind to
it; have a look at your shmem_engine_t definition - there's "readbuf"
and "writebuf" there - with original TCP transport the blocks are
allocated using malloc - with shmem they can be the actual shared memory
- existing implementation will store the data into writebuf when sending
messages, and extract messages from the readbuf when receiving
- all you have to do is to add a notification mechanism so that when
writebuf is full, the application sends a hint to the peer to start
extracting data from the buffer; other way round, when receiving side is
ready with reading the buffer it should notify the sender that it can
refill the buffer with new data; to send the notification use the TCP
connection established at the beginning
- to accomplish the previous point, note that shmem_engine_t::in_event
will be called when thare are data to receive on the TCP connection and
shmem_engine_t::out_event when data can be written to the connection.
That's about it, however, feel free to discuss the matter on the mailing
Btw, I recall there were other people asking for an IPC mechanism so you
may get some help with the implementation.
More information about the zeromq-dev