Java/C 共享内存 IPC 的良好替代方案 Linux 上的应用程序

发布于 2024-07-22 00:28:00 字数 103 浏览 9 评论 0原文

我目前正在 Java 和 C++ 应用程序之间使用 IPC 共享内存,但正在寻找更方便的替代方案。

有人可以建议具有相同性能和速度的更好方法吗?

谢谢!

I'm currently using shared memory for IPC between Java and C++ apps, but looking for a more convenient alternative.

Can someone advise a better method with same performance and speed?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

在梵高的星空下 2024-07-29 00:28:00

虽然可能不是最有效的,但 Java 仅支持开箱即用的套接字(我记得最好的)。 它们非常灵活,只是可能不如其他选项那么快。 正如 Zifre 提到的,它为您提供了网络透明度以及语言/绑定透明度的机会; 因为现在几乎每种语言都支持开箱即用的套接字库。

虽然我将效率抛之脑后,但如果您希望将其提升到一个新的水平,您可能可以将其包装在某种 Web 服务中。 在消费者上使用嵌入式 Web 服务器,以便生产者向其提交数据。

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.

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.

甜`诱少女 2024-07-29 00:28:00

正如麦克隆所说,这很大程度上取决于你在做什么。 AFAIK,没有一个 IPC 方法具有本机 Java 绑定,因此您可能必须使用 JNI 并自己进行绑定,因此所有不同的方法大致同样困难。 如果您正在进行消息传递,我强烈建议使用消息队列。 它们非常易于使用(一旦您拥有绑定),并且具有良好的性能。 如果您需要“共享”某些资源,那么您可能希望坚持使用共享内存。

听起来你有某种客户端/服务器的东西,我会说使用消息队列、unix 域套接字或命名管道。 它们都涉及在内核中复制数据,因此它们不像共享内存那么快,但它们仍然非常快。 如果您有类似消息的数据(单个小数据包),请使用消息队列。 这可能是最干净的解决方案。 如果您有更多的数据流,请使用管道或套接字。 套接字的优点是,如果您愿意,您可以在以后轻松地使其网络透明(如 X11),但它们比管道稍微难以使用。 性能可能非常相似。

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.

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.

愁以何悠 2024-07-29 00:28:00

这取决于您计划如何让应用程序交互。 在 POSIX 环境中,有管道、共享内存、套接字、信号量和消息队列。 请参阅此问题:比较 unix linux IPC 了解更多信息。

您的流程的交互模型是什么(即客户端/服务器、生产者-消费者等)?

根据个人经验,我建议您最好的选择是管道(因为它们只是读取和写入字节的文件)或套接字(因为两种语言都支持它们)。

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 IPC for more information.

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).

情栀口红 2024-07-29 00:28:00

在用不同语言编写的应用程序之间进行通信的最简单方法是 IMHO CORBA。 有非常好的开源CORBA ORB。 我们对 C++ 使用 TAO ,对 JacORB 用于 Java。 还有像 OCI提供技术支持的补救措施

The easiest way to communicate between applications written in different languages is IMHO CORBA. There are very good open source CORBA ORBs out there. We use TAO for C++ and JacORB for Java. There are also companies like OCI and Remedy that provide technical support.

清风挽心 2024-07-29 00:28:00

我目前正在 Java 和 C++ 应用程序之间使用共享内存进行 IPC,
但正在寻找更方便的替代方案。

有人可以建议更好的方法,但具有相同的性能速度吗?

对于简单的共享内存,您甚至不需要特殊的库:

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);
    }
}

局部变量 x 可以在线程外部和内部访问,允许您使用它将信息传入和传出线程。

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?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文