Linux IPC - 多个写入器,单个读取器 [英] Linux IPC - Multiple writers, single reader

查看:130
本文介绍了Linux IPC - 多个写入器,单个读取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有在Linux上编写任何IPC C ++。

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

我的问题是我将有多个客户端(写入器)和一个服务器所有这些将在同一台机器上。写者将向读者提供大块数据(字符串/结构体)。然后读取器将在FIFO中读取它们并与它们进行操作。

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.

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

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?

感谢

推荐答案

你应该考虑的主要问题是你传递什么样的数据,因为这将部分决定你的选择。这取决于你的数据是否有界。如果它没有边界,那么面向FIFO或套接字的流是适当的;如果它是,那么你可以更好地利用诸如MQ或共享内存。因为你提到字符串和结构体,很难说在你的情况下什么是合适的,但如果你的字符串在一定的合理的最大限度内,你可以使用任何与一些小的fiddling。

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.

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

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.

第三个是易用性。共享内存是最大的PITA,因为你必须处理自己的同步。只要您的邮件长度保持低于PIPE_BUF大小,管道就很容易。操作系统处理大多数你的头痛与MQs。套接字很容易,但你有设置样板。

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.

最后,几个IPC机制都有POSIX和SYSV变体。一般来说,POSIX是要走的路,除非SYSV类型有一些你真正需要或想要的功能。

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.

编辑:Count0的回答提醒我,你可能对一些更抽象和更高水平。除了ACE,您还可以查看 Poco 。当然,没有SO回答是完整的,如果它没有提及 Boost

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.

这篇关于Linux IPC - 多个写入器,单个读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆