Linux IPC - 多个写入器,单个读取器

发布于 2024-08-04 18:11:29 字数 269 浏览 2 评论 0原文

我以前从未在 Linux 上编写过任何 IPC C++。

我的问题是我将有多个客户端(编写器)和一个服务器(读取器)。所有这些都将在同一台机器上。作者将向读者传递数据块(字符串/结构)。然后,读取器将在 FIFO 中读取它们并对其执行某些操作。

据我所知,Linux 上的 IPC 类型要么是管道,要么是套接字/消息队列。

我只是想知道是否有人可以推荐我一条走下去的路。我倾向于套接字,但我没有真正的基础。在开始这段旅程之前,我应该阅读/理解什么内容吗?

谢谢

I have never written any IPC C++ on Linux before.

My problem is that I will have multiple clients (writers), and a single server (reader). All of these will be on the same machine. The writers will deliver chunks of data (a string/struct) to the reader. The reader will then read them in FIFO and do something with them.

The types of IPC on Linux are either Pipes or Sockets/Message Queues as far as I can tell.

I was just wondering if someone could recommend me a path to go down. I'm leaning towards sockets, but I have no real basis for that. Is there anything I should read/understand before embarking on this journey?

Thanks

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

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

发布评论

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

评论(5

风为裳 2024-08-11 18:11:30

System V IPC 使用起来有些麻烦,但它是一项成熟、强大的技术。消息队列可能会执行您想要的操作并支持原子排队/出队。

套接字易于使用并且还支持通过网络进行通信。但是,它们不进行任何排队,因此您必须在服务器内编写队列管理代码。在 C++ 中使用套接字与在 C 中使用套接字并没有太大的不同。网上有很多这方面的指南和书籍,例如 Stevens 的“Unix 网络编程(第 1 卷)”,其中深入讨论了该主题。

System V IPC is somewhat fiddly to use but it is a mature, robust technology. Message queues would probably do what you want and support atomic queuing/de-queuing.

Sockets are easy to use and also support communication over a network. However, they do not do any queuing, so you would have to write the queue management code within your server. Using sockets with C++ is not vastly different to using them with C. There are plenty of guides to this on the net and books such as Stevens' 'Unix Network Programming (vol 1)' that cover this topic in some depth.

合久必婚 2024-08-11 18:11:30

套接字教程是一个入门的好地方。

然后,您需要深入了解线程和线程。互斥体此处

完成上述操作后,您就可以开始玩了;-)

A good place to get your feet wet is this sockets tutorial.

You'll then need to bone-up on threads & mutexes and here.

With the above you're all set to start playing ;-)

暖心男生 2024-08-11 18:11:30

虽然您没有要求书籍,并且因为上面的答案非常好,但我只建议您获取这两本大部头的副本:

UNIX Network Planning,Volume 2,Second Edition:Interprocess Communications,W Richard Stevens

UNIX 环境中的高级编程,第二版,W. Richard Stevens 和 Stephen A.

Rago对于这种编码,这两本书将帮助您解决遇到的任何困惑。

Though you've not asked for books, and because the answers above are so good, I'm only going to suggest you get your hands on copies of these two tomes:

UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, W. Richard Stevens

Advanced Programming in the UNIX Environment, Second Edition, W. Richard Stevens and Stephen A. Rago

There are inevitable ins & outs with this kind of coding, these two books will help you through whatever confusion you encounter.

娇纵 2024-08-11 18:11:30

尝试查看ACE(自适应通信环境)。 ACE 库是免费的,非常成熟且跨平台。不幸的是,没有好的文档,我会推荐这本书来寻找好的解决方案。您可以尝试查看本教程来获取对模式的感觉(在文档的末尾)。 ACE 使用大量模式来非常成功且高效地处理这些问题,尤其是在网络环境中,因此它应该是寻找良好模式和方法的良好开端。

特别是使用 Message_QueueAce_Task 允许执行您需要的操作。

Try to take a look at ACE (Adaptive Communication Environment). The ACE libraries are free available, very mature and cross platform. Unfortunately a good documentation is not, i would recommend this book to look for a good solution. You might try to take a look at this tutorial to get a feel of the patterns (at the end of the document). ACE uses a bunch of patterns to deal very successfully and efficient with those problems especially in a networked context, so it should be a good start to scope for good patterns and methods to use.

Especially Ace_Task using the Message_Queue allow to do what you need.

方觉久 2024-08-11 18:11:29

您应该考虑的主要问题是您传递的数据类型,因为这将部分决定您的选择。这取决于您的数据是否有界。如果它没有限制,那么像 FIFO 或套接字这样的面向流的东西是合适的;如果是的话,那么您可能会更好地利用 MQ 或共享内存之类的东西。由于您提到了字符串和结构,所以很难说什么适合您的情况,但如果您的字符串限制在某个合理的最大值内,您可以使用任何带有一些小摆弄的东西。

第二是速度。对此从来没有一个完全正确的答案,但通常它是这样的:共享内存、MQ、FiFO、域套接字、网络套接字。

第三是易用性。共享内存是最大的 PITA,因为您必须处理自己的同步。只要消息长度保持在 PIPE_BUF 大小以下,管道就很容易。操作系统通过 MQ 解决了大部分令人头疼的问题。套接字很简单,但您有设置样板。

最后,一些 IPC 机制同时具有 POSIX 和 SYSV 变体。一般来说,除非 SYSV 类型具有您真正需要或想要的某些功能,否则 POSIX 是最佳选择。

编辑:Count0 的回答提醒我,您可能对更抽象和更高层次的东西感兴趣。除了 ACE 之外,您还可以查看 Poco。当然,如果不提到

The main issue you should consider is what kind of data you are passing as this will in part determine your options. This comes down to whether your data is bounded or not. If it isn't bounded then something stream oriented like FIFOs or sockets are appropriate; if it is then you might make better use of of things like MQs or shared memory. Since you mention both strings and structs it is hard to say what is appropriate in your case, though if your strings are bounded within some reasonable maximum you can use anything with some minor fiddling.

The second is speed. There is never a completely correct answer for this but generally it goes something like: shared memory, MQs, FiFOs, domain sockets, network sockets.

The third is ease of use. Shared memory is the biggest PITA since you have to handle your own synchronization. Pipes are easy so long as your message lengths stay below PIPE_BUF size. The OS handles most of your headaches with MQs. Sockets are easy enough but you have the setup boilerplate.

Lastly several of the IPC mechanisms have both POSIX and SYSV variants. Generally POSIX is the way to go unless the SYSV type has some feature you really need or want.

EDIT: Count0's answer reminded me that you might be interested in something more abstract and higher level. In addition to ACE you can look at Poco. And, of course, no SO answer is complete if it doesn't mention Boost somewhere.

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