当基础物理内存用尽时,如何处理访问mmap-ed内存的总线错误? [英] How to handle bus error for accessing mmap-ed memory when underlying physical memory runs out?

查看:99
本文介绍了当基础物理内存用尽时,如何处理访问mmap-ed内存的总线错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用"shm_open"创建共享内存对象,然后使用"mmap"将其映射到内存区域.但是,稍后,当代码实际访问内存时,在某些特殊情况下,由于基础物理内存用完,它将遇到总线错误".

we use "shm_open" to create a shared memory object, and then "mmap" to map it to a memory region. However, in later time, when the code actually accesses the memory, in some corner cases, it will hit "bus error" as the underlying physical memory was running out.

这在Linux中似乎很普通,因为"mmap"仅映射虚拟内存地址空间,并且仅当您访问页面时系统才分配实际的物理内存.

This seems to be a generic thing in Linux as the "mmap" only map the virtual memory address space and the system allocates the actual physical memory only when you access the pages.

我的问题是:我应该如何优雅地处理这种例外"?最佳做法是什么?我不希望底层内存用尽时程序崩溃,我想在这种情况下返回ENOMEM.有办法实现吗?

My question is: how should I handle such "exception" gracefully? What are the best practices? I don't want the program crashes when the underlying memory runs out, I wanted to return ENOMEM in such case. Is there a way to achieve that?

谢谢.

推荐答案

在Linux(带有glibc)上,shm_open创建的共享内存对象的实现是/dev/shm中的实际文件.通常情况下,在该位置安装了带有默认选项的tmpfs,即其最大大小为物理内存的一半.如果那还不够的话,您可以创建文件以在其他有更多可用空间的地方进行映射,而不必使用shm_open.

On Linux (with glibc) the implementation the shared memory objects shm_open creates are actual files in /dev/shm. Under normal circumstances there is a tmpfs mounted at that location with default options, i.e. its maximum size is half the physical memory. If that is not enough, instead of using shm_open you can create files to be mmap'ed elsewhere where there is more space available.

当您遇到这样的异常时,您几乎无能为力.特别是,您不能仅仅返回ENOMEM之类的东西,因为该异常是由对导致失败分配的mmap'ed区域的任何写入引起的.从字面上看,这可以是您在该区域中的任何代码编写,并且编程语言中没有简单的内存访问失败的概念,而处理这种情况的方法要少得多.

There is little you can do when you get such an exception. In particular you can't just return ENOMEM or something because the exception is caused by whatever write to the mmap'ed region that caused the failed allocation. That can be literally any write in your code to that area and there is no concept in programming languages of simple memory accesses failing, much less means to handle such a situation.

这篇关于当基础物理内存用尽时,如何处理访问mmap-ed内存的总线错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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