本文介绍了POSIX rlimit:关于RLIMIT_DATA,我们到底可以假定什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

POSIX.1 2008 setrlimit()getrlimit()函数.为 resource 参数提供了各种常量,为了便于理解我的问题,下面将复制其中一些常量.

POSIX.1 2008 specifies the setrlimit() and getrlimit() functions. Various constants are provided for the resource argument, some of which are reproduced below for easier understaning of my question.

(...)

这是进程的数据段的最大大小,以字节为单位.如果超过此限制,则malloc()函数将失败,且errno设置为[ENOMEM].

This is the maximum size of a data segment of the process, in bytes. If this limit is exceeded, the malloc() function shall fail with errno set to [ENOMEM].

(...)

这是初始线程堆栈的最大大小,以字节为单位.该实现不会自动使堆栈超出此限制.如果超出此限制,则将为该线程生成SIGSEGV.如果线程阻塞了SIGSEGV,或者该进程忽略或捕获了SIGSEGV,并且尚未安排使用备用堆栈,则应在生成SIGSEGV之前将其处置设置为SIG_DFL.

This is the maximum size of the initial thread's stack, in bytes. The implementation does not automatically grow the stack beyond this limit. If this limit is exceeded, SIGSEGV shall be generated for the thread. If the thread is blocking SIGSEGV, or the process is ignoring or catching SIGSEGV and has not made arrangements to use an alternate stack, the disposition of SIGSEGV shall be set to SIG_DFL before it is generated.

这是进程的总可用内存的最大大小,以字节为单位.如果超出了此限制,则将errno设置为[ENOMEM]时,malloc()和mmap()函数将失败.此外,自动堆栈增长因上面列出的效果而失败.

This is the maximum size of total available memory of the process, in bytes. If this limit is exceeded, the malloc() and mmap() functions shall fail with errno set to [ENOMEM]. In addition, the automatic stack growth fails with the effects outlined above.

此外,POSIX.1 2008 定义 数据段像这样:

Furthermore, POSIX.1 2008 defines data segment like this:

我知道RLMIT_DATA资源在传统上是用来表示可以通过brk()函数分配给进程的最大内存量. POSIX.1的最新版本不再指定此功能,并且许多操作系统(例如Mac OS X)都不支持此功能作为系统调用.相反,它使用mmap()的变体进行仿真,该变体不属于POSIX.1 2008.

I understand that the RLMIT_DATA resource was traditionally used to denote the maximum amount of memory that can be assigned to a process with the brk() function. Recent editions of POSIX.1 do no longer specify this function and many operating systems (e.g. Mac OS X) do not support this function as a system call. Instead it is emulated with a variant of mmap() which is not part of POSIX.1 2008.

对于RLIMIT_DATA资源的语义和使用,我有些困惑.这是我的具体问题:

I am a little bit confused about the semantic and use of the RLIMIT_DATA resource. Here are the concrete questions I have:

  • 根据该规范,堆栈可以成为数据段的一部分吗?

  • Can the stack be part of the data segment according to this specification?

该标准对RLIMIT_DATA进行了说明:如果超出此限制,则将errno设置为[ENOMEM]时,malloc()函数将失败."这是否意味着用malloc()分配的内存必须是数据段的一部分?

The standard says about RLIMIT_DATA: "If this limit is exceeded, the malloc() function shall fail with errno set to [ENOMEM]." Does this mean that memory allocated with malloc() must be part of the data segment?

在Linux上,用mmap()分配的内存不计入数据段.只有用brk()sbrk()分配的内存才是数据段的一部分. glibc的最新版本使用malloc()实现,该实现使用mmap()分配其所有内存.因此,RLIMIT_DATA的值对使用malloc()的这种实现可以分配的内存量没有影响.

On Linux, memory allocated with mmap() does not count towards the data segment. Only memory allocated with brk() or sbrk() is part of the data segment. Recent versions of the glibc use a malloc() implementation that allocates all its memory with mmap(). The value of RLIMIT_DATA thus has no effect on the amount of memory you can allocate with this implementation of malloc().

这是否违反POSIX.1 2008?

Is this a violation of POSIX.1 2008?

其他平台是否表现出类似的行为?

Do other platforms exhibit similar behavior?

该标准对RLIMIT_AS进行了说明:如果超出此限制,则errno设置为[ENOMEM]时,malloc()和mmap()函数将失败."由于没有为RLIMIT_DATA指定mmap()的失败,因此我得出结论,从mmap()获得的内存不计入数据段.

The standard says about RLIMIT_AS: "If this limit is exceeded, the malloc() and mmap() functions shall fail with errno set to [ENOMEM]." As the failure of mmap() is not specified for RLIMIT_DATA, I conclude that memory obtained from mmap() does not count towards the data segment.

此假设是否正确?这仅适用于mmap()的非POSIX变体吗?

Is this assumption true? Does this only apply to non-POSIX variants of mmap()?

推荐答案

FreeBSD还存在默认malloc实现中使用mmap(2)实现malloc(3)的问题.当将产品从FreeBSD 6移植到7时,我遇到了这种情况.我们将每个进程的默认限制从RLIMIT_DATA = 512M更改为RLIMIT_VMEM = 512M,即将虚拟内存分配限制为512MB.

FreeBSD also shares the problem of malloc(3) being implemented using mmap(2) in the default malloc implementation. I ran into this when porting a product from FreeBSD 6 to 7, where the switch happened. We switched the default limit for each process from RLIMIT_DATA=512M to RLIMIT_VMEM=512M, i.e. limit the virtual memory allocation to 512MB.

关于这是否违反POSIX,我不知道.我的直觉是,很多事情都违反了POSIX,并且100%兼容POSIX的系统与严格确认的C编译器一样罕见.

As for whether this violates POSIX, I don't know. My gut feeling is that lots of things violate POSIX and a 100% POSIX compliant system is as rare as a strictly-confirming C compiler.

嘿,现在我看到FreeBSD的名称RLIMIT_VMEM是非标准的;为了实现POSIX兼容性,他们将RLIMIT_AS定义为RLIMIT_VMEM.

heh, and now I see that FreeBSD's name RLIMIT_VMEM is non-standard; they define RLIMIT_AS as RLIMIT_VMEM for POSIX compatibility.

这篇关于POSIX rlimit:关于RLIMIT_DATA,我们到底可以假定什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:03
查看更多