Using MappedByteBuffer for shared memory

I read in Practical Game Programming in Java that MappedByteBuffers could be used for passing data between processes. If I wanted to use such a buffer to pass data between processes, what’s a good way of notifying the other process that there’s new data to be read from the buffer?

Brian

This article (scroll down to “Memory-Mapped Files”) explains the situation fairly well. Bascially, using Mapped files for RPC is not guaranteed functionality, and thus is not recommended. You also don’t know what kind of delay there might be between updates of the memory block (for systems where the block is not identical memory locations).

None the less, your problem can be solved with polling. The exact way to handle things depends upon what you want to do. i.e. Are you using a server to push to a set of client processes, or are you attempting back and forth communication?

With the former case, you could have a four byte header (an int) at the top of the block, with a packet counter. If the packet counter increases, your program knows that the data has changed. If you need the writes to be synchronous, you can use an extra byte per process as an “ACKnowledge” flag. When the server process writes the data, it sets the ACK flag to 0. After the receiving process is done reading, it sets the flag to 1.

A similar approach can be taken in the later case. Instead of using one block, however, I recommend that you make use of two blocks. Each process with the server of one block and the client of the other.

Does that help?

Edit: RPC should be IPC. RPC wouldn’t work very well this way. :wink:

Helps alot – thanks much! Brian