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 seterlimit64 are defined in
`sys/resource.h'.
*rlp.
The return value is 0 on success and -1 on failure. The
only possible errno error condition is EFAULT.
When the sources are compiled with _FILE_OFFSET_BITS == 64 on a
32-bit system this function is in fact getrlimit64. Thus, the
LFS interface transparently replaces the old interface.
getrlimit but its second parameter is
a pointer to a variable of type struct rlimit64, which allows it
to read values which wouldn't fit in the member of a struct
rlimit.
If the sources are compiled with _FILE_OFFSET_BITS == 64 on a
32-bit machine, this function is available under the name
getrlimit and so transparently replaces the old interface.
*rlp.
The return value is 0 on success and -1 on failure. The
following errno error condition is possible:
EPERM
When the sources are compiled with _FILE_OFFSET_BITS == 64 on a
32-bit system this function is in fact setrlimit64. Thus, the
LFS interface transparently replaces the old interface.
setrlimit but its second parameter is
a pointer to a variable of type struct rlimit64 which allows it
to set values which wouldn't fit in the member of a struct
rlimit.
If the sources are compiled with _FILE_OFFSET_BITS == 64 on a
32-bit machine this function is available under the name
setrlimit and so transparently replaces the old interface.
getrlimit to receive limit values,
and with setrlimit to specify limit values for a particular process
and resource. It has two fields:
rlim_t rlim_cur
rlim_t rlim_max
For getrlimit, the structure is an output; it receives the current
values. For setrlimit, it specifies the new values.
For the LFS functions a similar type is defined in `sys/resource.h'.
rlimit structure above, but
its components have wider ranges. It has two fields:
rlim64_t rlim_cur
rlimit.rlim_cur, but with a different type.
rlim64_t rlim_max
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 section Operation Error Signals.
RLIMIT_FSIZE
SIGXFSZ. See section Operation Error Signals.
RLIMIT_DATA
RLIMIT_STACK
SIGSEGV signal.
See section Program Error Signals.
RLIMIT_CORE
RLIMIT_RSS
RLIMIT_MEMLOCK
RLIMIT_NPROC
fork will fail
with EAGAIN. See section Creating a Process.
RLIMIT_NOFILE
RLIMIT_OFILE
errno
EMFILE. See section 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.
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.a
If 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
SETFSIZE
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 and errno is set according to the reason:
EPERM
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
RLIMIT_CPU for setrlimit.
LIM_FSIZE
RLIMIT_FSIZE for setrlimit.
LIM_DATA
RLIMIT_DATA for setrlimit.
LIM_STACK
RLIMIT_STACK for setrlimit.
LIM_CORE
RLIMIT_COR for setrlimit.
LIM_MAXRSS
RLIMIT_RSS for setrlimit.
The return value is zero for success, and -1 with errno set
accordingly for failure:
EPERM
Go to the first, previous, next, last section, table of contents.