预分配内存并在分叉进程中使用。
我想在分叉进程之前在程序中预先分配足够大的内存,然后在分叉进程中进一步分配/使用该池中的内存。我遇到过一些内存分配器,如 Bget、Boost 等,但无法理解如何使用它们。
有没有一个最简单的,我可以使用像
poolhandle = poolallocate(pool_size)
然后在分叉进程中使用像
ptr = allocatefromPool(poolhandle,no_of_bytes)
这样的东西,然后如果我通过某个 IPC 将此指针传递给另一个进程,它应该是可访问的即使在那个过程中。
你能指出我正确的方向吗?如果 Boost 是可行的方法,您能给我一个如何使用它的示例吗?
I would like to pre-allocate sufficiently large amount of memory in a program before forking processes and then further allocate/use memory from this pool in the forked processes. I have come across some memory allocators like Bget, Boost etc but not able to understand how to use them.
Is there a simplest one out there which I can use like
poolhandle = poolallocate(pool_size)
Then in forked process use something like
ptr = allocatefromPool(poolhandle,no_of_bytes)
and then if I pass this pointer to another process through some IPC it should be accessible even in that process.
Can you point me in right direction ? If Boost is the way to go can you provide me an example on how to use it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老实说,最简单的方法是使用内存映射文件。然后你就可以通过寻找和写作来拯救事物,或者通过寻找和阅读来获得事物。
除此之外,始终存在参与者的概念,您根本不共享任何内存,只是在参与者之间发送消息,每个参与者都是自己数据存储的管理者。这更容易编程,特别是使用 ZeroMQ 等工具,使进程间和线程间消息传递变得如此简单。
如果将这两个想法结合起来,那么每个参与者进程都有一个它们拥有的内存映射文件区域,并且您可以来回传递消息来告诉它们要读或写什么。
有关内存映射文件作为共享内存的更多信息,请参阅此问题 Posix 共享内存与映射文件< /a>
这是关于 IPC 的一个,您需要多个进程来协调操作 IPC vs 域 sock vs 命名管道
如果您有大量数据需要移动那么您可能希望将数据保留在内存映射文件中并实现某种形式的无锁共享。使用关键字“无锁”与“共享内存”或“数据结构”结合使用,您可以在 Google 上搜索到很多内容。
Honestly, the easiest way to do this is to use a memory-mapped file. Then you just do seek and write to save things, or seek and read to get things.
Beyond that, there is always the concept of actors where you don't share any memory at all, just send messages between actors, each of whom is the curator of their own datastore. This is simpler to program, especially with tools like ZeroMQ that make the interprocess and interthread messaging so simple.
If you combine the two ideas then each actor process has an area of the memory mapped file which they own, and you pass messages back and forth to tell them what to read or write.
See this question for more on memory-mapped files as shared memory Posix shared memory vs mapped files
Here is one about IPC which you will need for multiple processes to coordinate actions IPC vs domain sock vs named pipes
If you have large blocks of data to move around in messages then you would probably want to leave the data in place in the memory-mapped file and implement some form of lock-free sharing. There is lots of stuff you can Google using the keyword lock-free in conjunction with "shared memory" or "data structures".