内容简介
#include <sys/time.h>%uA0#include <sys/resource.h>
int getrlimit(int%uA0resource, struct rlimit *rlim)%uA0
int setrlimit(int%uA0resource, const struct rlimit *rlim)
描述
getrlimit() 和setrlimit()%uA0获取和分别设置资源限制。每个资源都有一个相关的软,硬限制,由rlimit%uA0结构(rlim%uA0参数两者之定义%uA0getrlimit() 和%uA0setrlimit()):
struct rlimit { rlim_t rlim_cur /* Soft limit */ rlim_t rlim_max /* Hard limit (ceiling for rlim_cur) */ } |
The value%uA0RLIM_INFINITYꃞnotes no limit on a resource (both in the structure returned by%uA0getrlimit() and in the structure passed to%uA0setrlimit()).
resource%uA0must be one of:
标签 | 描述 | |
---|---|---|
RLIMIT_AS | ||
%uA0 | The maximum size of the process’s virtual memory (address space) in bytes. This limit affects calls to%uA0brk(2),%uA0mmap(2) andmremap(2), which fail with the error%uA0ENOMEM%uA0upon exceeding this limit. Also automatic stack expansion will fail (and generate a%uA0SIGSEGV%uA0that kills the process if no alternate stack has been made available via%uA0sigaltstack(2)). Since the value is a%uA0long, on machines with a 32-bit%uA0long%uA0either this limit is at most 2 GiB, or this resource is unlimited. | |
RLIMIT_CORE | ||
%uA0 | Maximum size of%uA0core%uA0file. When 0 no core dump files are created. When non-zero, larger dumps are truncated to this size. | |
RLIMIT_CPU | ||
%uA0 | CPU time limit in seconds. When the process reaches the soft limit, it is sent a%uA0SIGXCPU%uA0signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program. If the process continues to consume CPU time, it will be sentSIGXCPU%uA0once per second until the hard limit is reached, at which time it is sent%uA0SIGKILL. (This latter point describes Linux 2.2 through 2.6 behaviour. Implementations vary in how they treat processes which continue to consume CPU time after reaching the soft limit. Portable applications that need to catch this signal should perform an orderly termination upon first receipt of%uA0SIGXCPU.) | |
RLIMIT_DATA | ||
%uA0 | The maximum size of the process’s data segment (initialized data, uninitialized data, and heap). This limit affects calls tobrk() and%uA0sbrk(), which fail with the error%uA0ENOMEM%uA0upon encountering the soft limit of this resource. | |
RLIMIT_FSIZE | ||
%uA0 | The maximum size of files that the process may create. Attempts to extend a file beyond this limit result in delivery of a%uA0SIGXFSZsignal. By default, this signal terminates a process, but a process can catch this signal instead, in which case the relevant system call (e.g.,%uA0write()%uA0truncate()) fails with the error%uA0EFBIG. | |
RLIMIT_LOCKS%uA0(Early Linux 2.4 only) | ||
%uA0 | A limit on the combined number of%uA0flock() locks and%uA0fcntl() leases that this process may establish. | |
RLIMIT_MEMLOCK | ||
%uA0 | The maximum number of bytes of memory that may be locked into RAM. In effect this limit is rounded down to the nearest multiple of the system page size. This limit affects%uA0mlock(2) andmlockall(2) and the%uA0mmap(2)%uA0MAP_LOCKED%uA0operation. Since Linux 2.6.9 it also affects the%uA0shmctl(2)%uA0SHM_LOCK%uA0operation, where it sets a maximum on the total bytes in shared memory segments (see%uA0shmget(2)) that may be locked by the real user ID of the calling process. The%uA0shmctl(2)%uA0SHM_LOCK%uA0locks are accounted for separately from the per-process memory locks established by%uA0mlock(2),%uA0mlockall(2), and%uA0mmap(2)MAP_LOCKED a process can lock bytes up to this limit in each of these two categories. In Linux kernels before 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process. Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock. | |
RLIMIT_MSGQUEUE%uA0(Since Linux 2.6.8) | ||
%uA0 |
Specifies the limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process. This limit is enforced for%uA0mq_open(3). Each message queue that the user creates counts (until it is removed) against this limit according to the formula:
The first addend in the formula, which includes%uA0sizeof(struct msg_msg *)%uA0(4 bytes on Linux/x86), ensures that the user cannot create an unlimited number of zero-length messages (such messages nevertheless each consume some system memory for bookkeeping overhead). |
|
RLIMIT_NICE%uA0(since kernel 2.6.12, but see BUGS below) | ||
%uA0 | Specifies a ceiling to which the process’s nice value can be raised using%uA0setpriority(2) or%uA0nice(2). The actual ceiling for the nice value is calculated as%uA020 - rlim_cur. (This strangeness occurs because negative numbers cannot be specified as resource limit values, since they typically have special meanings. For example, RLIM_INFINITY typically is the same as -1.) | |
RLIMIT_NOFILE | ||
%uA0 | Specifies a value one greater than the maximum file descriptor number that can be opened by this process. Attempts (open(),pipe(),%uA0dup(), etc.) to exceed this limit yield the error%uA0EMFILE. | |
RLIMIT_NPROC | ||
%uA0 | The maximum number of threads that can be created for the real user ID of the calling process. Upon encountering this limit,fork() fails with the error%uA0EAGAIN. | |
RLIMIT_RSS | ||
%uA0 | Specifies the limit (in pages) of the process’s resident set (the number of virtual pages resident in RAM). This limit only has effect in Linux 2.4.x, x < 30, and there only affects calls tomadvise() specifying%uA0MADV_WILLNEED. | |
RLIMIT_RTPRIO%uA0(Since Linux 2.6.12, but see BUGS) | ||
%uA0 | Specifies a ceiling on the real-time priority that may be set for this process using%uA0sched_setscheduler(2) andsched_setparam(2). | |
RLIMIT_SIGPENDING%uA0(Since Linux 2.6.8) | ||
%uA0 | Specifies the limit on the number of signals that may be queued for the real user ID of the calling process. Both standard and real-time signals are counted for the purpose of checking this limit. However, the limit is only enforced for%uA0sigqueue(2) it is always possible to use%uA0kill(2) to queue one instance of any of the signals that are not already queued to the process. | |
RLIMIT_STACK | ||
%uA0 | The maximum size of the process stack, in bytes. Upon reaching this limit, a%uA0SIGSEGV%uA0signal is generated. To handle this signal, a process must employ an alternate signal stack (sigaltstack(2)). |
返回值
On success, zero is returned. On error, -1 is returned, and%uA0errno%uA0is set appropriately.错误
标签 | 描述 |
---|---|
EFAULT | rlim%uA0points outside the accessible address space. |
EINVAL | resource%uA0is not valid or, for%uA0setrlimit():%uA0rlim->rlim_cur%uA0was greater than%uA0rlim->rlim_max. |
EPERM | An unprivileged process tried to use%uA0setrlimit() to increase a soft or hard limit above the current hard limit theCAP_SYS_RESOURCEꃊpability is required to do this. Or, the process tried to use%uA0setrlimit() to increase the soft or hard RLIMIT_NOFILE limit above the current kernel maximum (NR_OPEN). |
BUGS
In older Linux kernels, the%uA0SIGXCPU%uA0and%uA0SIGKILL%uA0signals delivered when a process encountered the soft and hard%uA0RLIMIT_CPU%uA0limits were delivered one (CPU) second later than they should have been. This was fixed in kernel 2.6.8.In 2.6.x kernels before 2.6.17, a%uA0RLIMIT_CPU%uA0limit of 0 is wrongly treated as "no limit" (like%uA0RLIM_INFINITY). Since kernel 2.6.17, setting a limit of 0 does have an effect, but is actually treated as a limit of 1 second.
A kernel bug means that%uA0RLIMIT_RTPRIO%uA0does not work in kernel 2.6.12 the problem is fixed in kernel 2.6.13.
In kernel 2.6.12, there was an off-by-one mismatch between the priority ranges returned by%uA0getpriority(2) and%uA0RLIMIT_NICE. This had the effect that actual ceiling for the nice value was calculated as%uA019 - rlim_cur. This was fixed in kernel 2.6.13.
Kernels before 2.4.22 did not diagnose the error%uA0EINVAL%uA0for%uA0setrlimit() when%uA0rlim->rlim_cur%uA0was greater than%uA0rlim->rlim_max.