You can specify limits for the resource usage of a process. When the process tries to exceed a limit, it may get a signal, or the system call by which it tried to do so may fail, depending on the resource. Each process initially inherits its limit values from its parent, but it can subsequently change them.
There are two per-process limits associated with a resource:
The symbols for use with getrlimit
, setrlimit
,
getrlimit64
, and setrlimit64
are defined in
sys/resource.h.
Read the current and maximum limits for the resource resource and store them in
*
rlp.The return value is
0
on success and-1
on failure. The only possibleerrno
error condition isEFAULT
.When the sources are compiled with
_FILE_OFFSET_BITS == 64
on a 32-bit system this function is in factgetrlimit64
. Thus, the LFS interface transparently replaces the old interface.
This function is similar to
getrlimit
but its second parameter is a pointer to a variable of typestruct rlimit64
, which allows it to read values which wouldn't fit in the member of astruct rlimit
.If the sources are compiled with
_FILE_OFFSET_BITS == 64
on a 32-bit machine, this function is available under the namegetrlimit
and so transparently replaces the old interface.
Store the current and maximum limits for the resource resource in
*
rlp.The return value is
0
on success and-1
on failure. The followingerrno
error condition is possible:
EPERM
- The process tried to raise a current limit beyond the maximum limit.
- The process tried to raise a maximum limit, but is not superuser.
When the sources are compiled with
_FILE_OFFSET_BITS == 64
on a 32-bit system this function is in factsetrlimit64
. Thus, the LFS interface transparently replaces the old interface.
This function is similar to
setrlimit
but its second parameter is a pointer to a variable of typestruct rlimit64
which allows it to set values which wouldn't fit in the member of astruct rlimit
.If the sources are compiled with
_FILE_OFFSET_BITS == 64
on a 32-bit machine this function is available under the namesetrlimit
and so transparently replaces the old interface.
This structure is used with
getrlimit
to receive limit values, and withsetrlimit
to specify limit values for a particular process and resource. It has two fields:
rlim_t rlim_cur
- The current limit
rlim_t rlim_max
- The maximum limit.
For
getrlimit
, the structure is an output; it receives the current values. Forsetrlimit
, it specifies the new values.
For the LFS functions a similar type is defined in sys/resource.h.
This structure is analogous to the
rlimit
structure above, but its components have wider ranges. It has two fields:
rlim64_t rlim_cur
- This is analogous to
rlimit.rlim_cur
, but with a different type.rlim64_t rlim_max
- This is analogous to
rlimit.rlim_max
, but with a different type.
Here is a list of resources for which you can specify a limit. Memory and file sizes are measured in bytes.
RLIMIT_CPU
SIGXCPU
. The value is
measured in seconds. See Operation Error Signals.
RLIMIT_FSIZE
SIGXFSZ
. See Operation Error Signals.
RLIMIT_DATA
RLIMIT_STACK
SIGSEGV
signal.
See Program Error Signals.
RLIMIT_CORE
RLIMIT_RSS
RLIMIT_MEMLOCK
RLIMIT_NPROC
fork
will fail
with EAGAIN
. See Creating a Process.
RLIMIT_NOFILE
RLIMIT_OFILE
errno
EMFILE
. See Error Codes. Not all systems support this limit;
GNU does, and 4.4 BSD does.
RLIMIT_AS
brk
, malloc
, mmap
or sbrk
, the
allocation function fails.
RLIM_NLIMITS
RLIM_NLIMITS
.
This constant stands for a value of “infinity” when supplied as the limit value in
setrlimit
.
The following are historical functions to do some of what the functions above do. The functions above are better choices.
ulimit
and the command symbols are declared in ulimit.h.
ulimit
gets the current limit or sets the current and maximum limit for a particular resource for the calling process according to the command cmd.aIf you are getting a limit, the command argument is the only argument. If you are setting a limit, there is a second argument:
long int
limit which is the value to which you are setting the limit.The cmd values and the operations they specify are:
GETFSIZE
- Get the current limit on the size of a file, in units of 512 bytes.
SETFSIZE
- Set the current and maximum limit on the size of a file to limit * 512 bytes.
There are also some other cmd values that may do things on some systems, but they are not supported.
Only the superuser may increase a maximum limit.
When you successfully get a limit, the return value of
ulimit
is that limit, which is never negative. When you successfully set a limit, the return value is zero. When the function fails, the return value is-1
anderrno
is set according to the reason:
EPERM
- A process tried to increase a maximum limit, but is not superuser.
vlimit
and its resource symbols are declared in sys/vlimit.h.
vlimit
sets the current limit for a resource for a process.resource identifies the resource:
LIM_CPU
- Maximum CPU time. Same as
RLIMIT_CPU
forsetrlimit
.LIM_FSIZE
- Maximum file size. Same as
RLIMIT_FSIZE
forsetrlimit
.LIM_DATA
- Maximum data memory. Same as
RLIMIT_DATA
forsetrlimit
.LIM_STACK
- Maximum stack size. Same as
RLIMIT_STACK
forsetrlimit
.LIM_CORE
- Maximum core file size. Same as
RLIMIT_COR
forsetrlimit
.LIM_MAXRSS
- Maximum physical memory. Same as
RLIMIT_RSS
forsetrlimit
.The return value is zero for success, and
-1
witherrno
set accordingly for failure:
EPERM
- The process tried to set its current limit beyond its maximum limit.