C++ 和 Java 进程之间的共享内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15414753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:30:38  来源:igfitidea点击:

Shared memory between C++ and Java processes

javac++java-native-interfaceshared-memory

提问by Brent Campbell

My aim is to pass data from a C++ process to a Java process and then to receive a result back.

我的目标是将数据从 C++ 进程传递到 Java 进程,然后接收返回的结果。

I have achieved this via a named pipe but I would prefer to share the data rather than passing or copying it, assuming the access would be faster.

我已经通过命名管道实现了这一点,但我更愿意共享数据而不是传递或复制它,假设访问速度会更快。

Initially, I thought of creating a shared segment in C++ that I could write to and read with Java, but I'm not sure if this is possible via JNI, let alone safe.

最初,我想在 C++ 中创建一个共享段,我可以用 Java 写入和读取它,但我不确定这是否可以通过 JNI 实现,更不用说安全了。

I believe it's possible in Java to allocate the memory using ByteBuffer.allocateDirect and then use GetDirectBufferAddress to access the address in C++, but if I'm correct this is for native calls within JNI and I can't get this address in my C++ process?

我相信在 Java 中可以使用 ByteBuffer.allocateDirect 分配内存,然后使用 GetDirectBufferAddress 访问 C++ 中的地址,但如果我是正确的,这是针对 JNI 中的本机调用,我无法在我的 C++ 进程中获取此地址?

Lost.

丢失。

Many thanks in advance.

提前谢谢了。

回答by main--

If you have shared memory, for example using CreateFileMapping(Windows) or shmget(Unix), all you need is a native method on the Java side. Then you can create a ByteBufferthat directlyaccesses the shared memory using NewDirectByteBufferlike this:

如果您有共享内存,例如使用CreateFileMapping(Windows) 或shmget(Unix),您所需要的只是 Java 端的本机方法。然后,你可以创建一个ByteBuffer直接使用访问共享内存NewDirectByteBuffer是这样的:

JNIEXPORT jobject JNICALL Java_getSharedBuffer(JNIEnv* env, jobject caller) {
    void* myBuffer;
    int bufferLength;

Now you have to get a pointer to the shared memory. On Windows you would use something like this:

现在你必须得到一个指向共享内存的指针。在 Windows 上,你会使用这样的东西:

    bufferLength = 1024; // assuming your buffer is 1024 bytes big
    HANDLE mem = OpenFileMapping(FILE_MAP_READ, // assuming you only want to read
           false, "MyBuffer"); // assuming your file mapping is called "MyBuffer"
    myBuffer = MapViewOfFile(mem, FILE_MAP_READ, 0, 0, 0);
    // don't forget to do UnmapViewOfFile when you're finished

Now you can just create a ByteBufferthat is backed by this shared memory:

现在你可以创建一个ByteBuffer由这个共享内存支持的:

    // put it into a ByteBuffer so the java code can use it
    return env->NewDirectByteBuffer(myBuffer, bufferLength);
}

回答by Shafik Yaghmour

Have you considered using 0MQit supports both Javaand C++and will be more reliable. I think if you want to do shared memory in Java it would have to be via JNI, last time I looked there was not other way to do it.

您是否考虑过使用0MQ,它同时支持JavaC++,并且会更可靠。我想如果你想在 Java 中做共享内存,它必须通过 JNI,上次我看没有其他方法可以做到这一点。

This shows you have to do it via JNIif you go that route. Although the solutions I have found are Windows specific which may not apply to you.

这表明如果你走那条路,你必须通过JNI来做。尽管我找到的解决方案是特定于 Windows 的,可能不适用于您。