如何在 OSX 上创建没有备份文件的内存映射文件?

发布于 2024-09-17 16:52:11 字数 270 浏览 7 评论 0原文

我想使用一个使用文件描述符作为访问其数据的基本手段的库。出于性能原因,我不想在使用该库的功能之前每次都将文件提交到磁盘。

我想动态创建(大型)数据 blob,并调用库将它们发送到服务器。按照目前的情况,我必须将文件写入磁盘,打开它,将 FD 传递到库,等待它完成,然后删除磁盘上的文件。由于我可以根据需要重新创建 blob(而且它们不会太大而导致过多的虚拟内存分页),因此将它们保存到磁盘不会给我带来任何好处,而且会带来很大的性能损失。

是否可以将 FD 分配给仅作为内存映射实体驻留的数据块?

I want to use a library that uses file descriptors as the basic means to access its data. For performance reasons, I don't want to have to commit files to the disk each before I use this library's functions.

I want to create (large) data blobs on the fly, and call into the library to send them to a server. As it stands, I have to write the file to disk, open it, pass the FD to the library, wait for it to finish, then delete the file on disk. Since I can re-create the blobs on demand (and they're not so large that they cause excessive virtual memory paging), saving them to disk buys me nothing, and incurs a large performance penalty.

Is it possible to assign a FD to a block of data that resides only as a memory-mapped entity?

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

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

发布评论

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

评论(1

蘸点软妹酱 2024-09-24 16:52:11

您可以安装内存支持的文件系统http://lists.apple.com/archives/darwin-kernel/2004/Sep/msg00004.html

使用此机制会增加系统的内存压力,如果内存压力过大,可能会被换出页面足够棒了。如果用户希望其他应用程序优先选择内存,则将其作为配置选项可能是值得的。

另一种选择是使用POSIX共享内存段http:// (我自己没有使用过 POSIX 共享内存段;如果我理解正确的话,它们就是为了解决这个问题而设计的。)

/opengroup.org/onlinepubs/007908799/xsh/shm_open.html shm_open()函数创建一个内存对象并返回一个文件描述符。然后,您可以 mmap(2) 该文件描述符,完成您的工作,并将文件描述符传递给库。

完成后,不要忘记 shm_unlink 对象;当最后一个进程退出时,POSIX 共享内存段、消息队列和信号量数组不会自动消失。

You could mount a memory-backed filesystem: http://lists.apple.com/archives/darwin-kernel/2004/Sep/msg00004.html

Using this mechanism will increase memory pressure on the system, and will probably be paged out if memory pressure is great enough. It might be worthwhile to make it a configuration option, in case the user would rather some other application have first-choice of the memory.

Another option is to use POSIX shared memory segments: http://opengroup.org/onlinepubs/007908799/xsh/shm_open.html (I haven't used POSIX shared memory segments myself; if I understand them correctly, they were designed to solve exactly this problem.)

The shm_open() function creates a memory object and returns a file descriptor. You could then mmap(2) that file descriptor, do your work, and pass the file descriptor to the library.

Don't forget to shm_unlink the object when you're done; POSIX shared memory segments, message queues, and semaphore arrays don't automatically go away when the last process exits.

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