6

I have an application in C/C++ that writes data in shared memory in Unix. Now I want to read that shared memory data through a program using Java.

The program should read the shared memory data. Can it be possible?

Need a small implementation if possible.

j0k
  • 22,600
  • 28
  • 79
  • 90
Nikhil
  • 207
  • 1
  • 3
  • 15
  • 1
    Reading shared memory does if ever only work through JNI - in Java you can't do that. My experience with JNI was that some JNI-plugins crashed the otherwise rock-solid Java VM. So maybe another solution is possible? – luukes Jan 08 '13 at 08:38
  • This answer http://stackoverflow.com/a/1492536/1741542 talks about memory mapped files and NIO FileChannel. Maybe this works for you too. – Olaf Dietsche Jan 08 '13 at 08:54
  • If you would benefit from persistence of the data (even for debugging purposes) you could consider memory mapped files. Here an implementation which uses pure Java https://github.com/peter-lawrey/Java-Chronicle – Peter Lawrey Jan 08 '13 at 09:14
  • @luukes Are you sure that using a Java program we cannot read the shared memory data written by c++ program? And we have to have JNI in between to achieve this? – Nikhil Jan 09 '13 at 11:10

1 Answers1

5

Take a look at this codeproject project (For Windows):

Using Memory Mapped Files and JNI to communicate between Java and C++ programs

Or, this library:

CLIPC is an open-source Java library that gives developers interprocess communications (IPC) capabilities that may be absent or difficult to use in the Java distribution.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • Can I create a method in C++ only to read the shared memory, and then call this method using JNI? Would that be effective and won't harm to performance also? – Nikhil Jan 08 '13 at 11:47
  • JNI methods should call from Java not C++. Yes you can create a method in C++ to read/write from/to the shared memory. And also you can do same thing in Java with JNI methods. Don't worry about performance if you do it right. – masoud Jan 08 '13 at 12:43
  • Can you please just tell me the suggestions how can I achieve the below functionality? – Nikhil Jan 09 '13 at 09:51