C++ 多个进程?
我有一个由两个进程组成的项目,我需要以快速有效的方式在它们之间传递一些数据。
我知道我可以使用 TCP 来使用套接字来执行此操作,即使两个进程始终存在于同一台计算机上,但这似乎不是一个非常有效的解决方案。
我看到很多关于在 Linux 上使用“管道”的信息。 然而我主要希望它适用于 Windows 和 Linux(最好通过跨平台库),最好是类型安全的, 非阻塞方式。
另一件重要的事情是我需要支持整个应用程序的多个实例(即两个进程),每个实例都有自己独立的通信对象副本。
还有一种跨平台的方式来生成新进程吗?
I've got a project that consists of two processes and I need to pass some data between them in a fast and efficent manner.
I'm aware that I could use sockets to do this using TCP, even though both processes will always exist on the same computer, however this does not seem to be a very efficient solution.
I see lots of information about using "pipes" on Linux. However I primarily want this for Windows and Linux (preferably via a cross platform library), ideally in a type safe,
non-blocking manner.
Another important thing is I need to support multiple instances of the whole application (i.e. both processes), each with their own independent copy of the communication objects.
Also is there a cross platform way to spawn a new process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看 Boost.Interprocess
Take a look at Boost.Interprocess
对于 IPC,Windows 仅支持命名管道就像 Linux 一样,只是管道名称由于差异而遵循不同的格式两个操作系统之间的路径格式。 您可以通过简单的预处理器定义来克服这个问题。 两个操作系统还支持管道上的非阻塞 IO 以及使用 select() 的 IO 复用。
For IPC, Windows supports named pipes just like Linux does, except that the pipe names follow a different format, owing to the difference in path formats between the two operating systems. This is something that you could overcome with simple preprocessor defines. Both operating systems also support non-blocking IO on pipes and IO multiplexing with select().
普通的旧式 TCP 应该相当高效地工作; 据我了解,现代操作系统将检测 TCP 连接的两端何时位于同一台机器上,并通过快速、轻量级(类似管道)机制而不是通过普通的 TCP 堆栈在内部路由该数据。
因此,如果您已经拥有可通过 TCP 运行的代码,我建议您坚持下去,避免花费大量额外的开发时间而收效甚微。
Plain old TCP should work fairly efficiently; as I understand it, modern OS's will detect when both ends of a TCP connection are located on the same machine, and will internally route that data through a fast, lightweight (pipe-like) mechanism rather than through the ordinary TCP stack.
So if you already have code that works over TCP, I say stick with that and avoid spending a lot of extra development time for not much payoff.
这可能有些过头了,但您可以使用 Apache Portable Runtime; 这里是线程和进程函数。
It may be overkill, but you could use the Apache Portable Runtime; here are the thread and process functions.