Linux 上 Java/C++ 应用程序共享内存 IPC 的良好替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/904492/
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
Good alternative to shared memory IPC for Java/C++ apps on Linux
提问by SyRenity
I'm currently using shared memory for IPC between Java and C++ apps, but looking for a more convenient alternative.
我目前在 Java 和 C++ 应用程序之间为 IPC 使用共享内存,但正在寻找更方便的替代方案。
Can someone advise a better method with same performance and speed?
有人可以建议具有相同性能和速度的更好方法吗?
Thanks!
谢谢!
回答by mikelong
It depends on how you plan to have your apps interact. In the POSIX environment, you have pipes, shared memory, sockets, semaphores and message queues. See this question: Comparing unix linux IPCfor more information.
这取决于您计划如何让您的应用程序交互。在 POSIX 环境中,您有管道、共享内存、套接字、信号量和消息队列。请参阅此问题:比较 unix linux IPC以获取更多信息。
What is the interaction model for your processes (i.e. client/server, producer-consumer, etc)?
您的流程(即客户端/服务器、生产者-消费者等)的交互模型是什么?
From personal experience, I would suggest your best bet would be pipes (since they are just files to read and write bytes) or sockets (since both languages support them).
根据个人经验,我建议您最好的选择是管道(因为它们只是读取和写入字节的文件)或套接字(因为两种语言都支持它们)。
回答by Zifre
As mikelong said, this depends a lot on what you are doing. AFAIK, none of the IPC methods have native Java bindings, so you're probably going to have to use JNI and make bindings yourself, so all the different methods are roughly equally as hard. If you are doing message passing though, I highly suggest using message queues. They are very easy to use (once you have the bindings), and have good performance. If you need to "share" some resource, then you probably want to stick with shared memory.
正如麦克隆所说,这在很大程度上取决于你在做什么。AFAIK,没有一个 IPC 方法具有本机 Java 绑定,因此您可能必须使用 JNI 并自己进行绑定,因此所有不同的方法的难度大致相同。如果你在做消息传递,我强烈建议使用消息队列。它们非常易于使用(一旦您有了绑定),并且具有良好的性能。如果您需要“共享”某些资源,那么您可能希望坚持使用共享内存。
As it sounds like you are having some sort client/server thing, I would say use either message queues, unix domain sockets, or named pipes. They all involve copying data in the kernel, so they are not quite as fast as shared memory, but they are still very fast. If you have message-like data (individual small packets), go with message queues. That is probably the cleanest solution. If you have more of a stream of data, use pipes or sockets. Sockets have the advantage that you can easily make it network transparent (like X11) later on if you want, but they are slightly harder to work with than pipes. The performance is probably very similar.
听起来你有某种客户端/服务器的东西,我会说使用消息队列、unix 域套接字或命名管道。它们都涉及在内核中复制数据,因此它们不如共享内存快,但它们仍然非常快。如果您有类似消息的数据(单个小数据包),请使用消息队列。这可能是最干净的解决方案。如果您有更多的数据流,请使用管道或套接字。套接字的优点是您可以在以后轻松地使其网络透明(如 X11),但与管道相比,它们更难使用。性能可能非常相似。
回答by monceaux
While likely not the most efficient, Java only supports sockets out of the box (the best I recall). They are very flexible, just perhaps not as fast as other options. As Zifre mentioned, it leaves you with an opportunity for network transparency, as well as language/binding transparency; as pretty much every language supports a socket library out of the box these days.
虽然可能不是最有效的,但 Java 只支持开箱即用的套接字(我记得是最好的)。它们非常灵活,只是可能不如其他选项快。正如 Zifre 所提到的,它为您提供了网络透明度以及语言/绑定透明度的机会;因为现在几乎每种语言都支持开箱即用的套接字库。
While I am throwing efficiency out of the window, if you wish to take it to the next level, you could probably wrap this in a Web Service of some kind. Use an embedded web server on the consumer for the producers to submit their data to.
虽然我将效率抛诸脑后,但如果您希望将其提升到一个新的水平,您可能可以将其包装在某种 Web 服务中。在消费者上使用嵌入式 Web 服务器,供生产者向其提交数据。
回答by lothar
The easiest way to communicate between applications written in different languages is IMHO CORBA. There are very good open source CORBA ORBsout there. We use TAOfor C++ and JacORBfor Java. There are also companies like OCIand Remedythat provide technical support.
在用不同语言编写的应用程序之间进行通信的最简单方法是 IMHO CORBA。那里有非常好的开源CORBA ORB。我们在 C++ 中使用TAO,在 Java 中使用JacORB。还有像OCI和Remedy这样的公司提供技术支持。
回答by zjagannatha
I'm currently using shared memory for IPC between Java and C++ apps, but looking for a more convenient alternative.
Can someone advice better method, but with same performance speed?
我目前在 Java 和 C++ 应用程序之间为 IPC 使用共享内存,但正在寻找更方便的替代方案。
有人可以建议更好的方法,但具有相同的性能速度吗?
For simple shared memory you don't even need a special library:
对于简单的共享内存,您甚至不需要特殊的库:
class Main {
private static class CustomThread extends Thread {
public int x = 0;
public void run() {
x = 5;
}
}
public static void main(String[] args) {
CustomThread t = new CustomThread();
t.start();
System.out.println(t.x);
System.out.println(t.x);
}
}
The local variable x be accessed outside the thread and within, allowing you to use it to pass information in and out of the thread.
局部变量 x 可以在线程外部和内部访问,允许您使用它来将信息传入和传出线程。

