c++ 中父子进程之间共享队列

发布于 2024-09-09 08:09:24 字数 85 浏览 0 评论 0原文

我知道有很多方法可以处理两个进程之间的相互通信,但我仍然有点困惑如何处理它。是否可以在两个进程之间以有效的方式共享队列(来自标准库)?

谢谢

I know there are many way to handle inter-communication between two processes, but I'm still a bit confused how to deal with it. Is it possible to share queue (from standard library) between two processes in efficient way?

Thanks

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

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

发布评论

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

评论(3

謸气贵蔟 2024-09-16 08:09:24

我相信您的困惑来自于不理解父进程和子进程的内存地址空间之间的关系。这两个地址空间实际上是不相关的。是的,在 fork() 之后,两个进程立即包含几乎相同的内存副本,但您应该将它们视为副本。一个进程对其地址空间中的内存所做的任何更改都不会影响另一进程的内存。

任何“普通旧数据结构”(例如 C++ 标准库提供的)都是纯粹的内存抽象,因此无法使用它们在两个进程之间进行通信。要将数据从一个进程发送到另一进程,必须使用提供进程间通信的多个系统调用之一。

但是,请注意共享内存是一个例外。您可以使用系统调用来设置一段共享内存,然后在共享内存中创建数据结构。您仍然需要使用互斥锁来保护这些数据结构,但互斥锁必须是共享内存感知的。对于 Posix 线程,您可以将 pthread_mutexattr_init 与 PTHREAD_PROCESS_SHARED 属性一起使用。

I believe your confusion comes from not understanding the relationship between the memory address spaces of the parent and child process. The two address spaces are effectively unrelated. Yes, immediately after the fork() the two processes contain almost identical copies of memory, but you should think of them as copies. Any change one proces makes to memory in its address space has no impact on the other process's memory.

Any "plain old data structures" (such as provided by the C++ standard library) are purely abstractions of memory, so there is no way to use them to communicate between the two processes. To send data from one process to the other, you must use one of several system calls that provide interprocess communication.

But, note that shared memory is an exception to this. You can use system calls to set up a section of share memory, and then create data structures in the share memory. You'll still need to protect these data structures with a mutex, but the mutex will have to be shared-memory aware. With Posix threads, you'd use pthread_mutexattr_init with the PTHREAD_PROCESS_SHARED attribute.

脱离于你 2024-09-16 08:09:24

简单的答案:可以由两个进程共享 std::queue ,但这并不容易。

您可以使用共享内存和某种同步机制(通常是互斥体)将队列保存在一起。请注意,不仅必须在共享内存区域中构造 std::queue 对象,还必须构造队列的内容,因此您必须提供自己的分配器来管理共享内存区域中的内存创建共享区域。

如果可以的话,尝试查看更高级别的库,它们可能会为您的过程通信需求提供已经打包的解决方案。考虑 Boost.Interprocess 或在您最喜欢的搜索中搜索进程间通信的引擎。

Simple answer: Sharing an std::queue by two processes can be done but it is not trivial to do.

You can use shared memory to hold the queue together with some synchronization mechanism (usually a mutex). Note that not only the std::queue object must be constructed in the shared memory region, but also the contents of the queue, so you will have to provide your own allocator that manages the creation of memory in the shared region.

If you can, try to look at higher level libraries that might provide already packed solutions to your process communication needs. Consider Boost.Interprocess or search in your favorite search engine for interprocess communication.

浪菊怪哟 2024-09-16 08:09:24

我认为没有任何简单的方法可以在两个项目之间共享结构/对象。如果要在两个进程之间实现队列/列表/数组等,则需要在进程之间实现某种通信来管理队列以及检索和存储条目。

例如,您可以在一个进程中实现队列管理,并实现某种 IPC(共享内存、套接字、管道等)以将条目从一个进程移交给另一个进程。

标准 C++ 库之外可能还有其他方法可以为您执行此操作。例如,可能有 Boost 库已经实现了这一点。

I don't think there are any simple ways to share structures/objects like that between two projects. If you want to implement a queue/list/array/etc between two processes, you will need to implement some kind of communication between the processes to manage the queues and to retrieve and store entries.

For example, you could implement the queue management in one process and implement some kind of IPC (shared memory, sockets, pipes, etc.) to hand off entries from one process to the other.

There may be other methods outside of the standard C++ libraries that will do this for you. For example, there are likely Boost libraries that already implement this.

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