Next: Priority, Previous: Resource Usage, Up: Resource Usage And Limitation
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
0on success and-1on failure. The only possibleerrnoerror condition isEFAULT.When the sources are compiled with
_FILE_OFFSET_BITS == 64on a 32-bit system this function is in factgetrlimit64. Thus, the LFS interface transparently replaces the old interface.
This function is similar to
getrlimitbut 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 == 64on a 32-bit machine, this function is available under the namegetrlimitand so transparently replaces the old interface.
Store the current and maximum limits for the resource resource in
*rlp.The return value is
0on success and-1on failure. The followingerrnoerror 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 == 64on a 32-bit system this function is in factsetrlimit64. Thus, the LFS interface transparently replaces the old interface.
This function is similar to
setrlimitbut its second parameter is a pointer to a variable of typestruct rlimit64which allows it to set values which wouldn't fit in the member of astruct rlimit.If the sources are compiled with
_FILE_OFFSET_BITS == 64on a 32-bit machine this function is available under the namesetrlimitand so transparently replaces the old interface.
This structure is used with
getrlimitto receive limit values, and withsetrlimitto 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
rlimitstructure 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_CPUSIGXCPU. The value is
measured in seconds. See Operation Error Signals.
RLIMIT_FSIZESIGXFSZ. See Operation Error Signals.
RLIMIT_DATARLIMIT_STACKSIGSEGV signal.
See Program Error Signals.
RLIMIT_CORERLIMIT_RSSRLIMIT_MEMLOCKRLIMIT_NPROCfork will fail
with EAGAIN. See Creating a Process.
RLIMIT_NOFILERLIMIT_OFILEerrno
EMFILE. See Error Codes. Not all systems support this limit;
GNU does, and 4.4 BSD does.
RLIMIT_ASbrk, malloc, mmap or sbrk, the
allocation function fails.
RLIM_NLIMITSRLIM_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.
ulimitgets 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 intlimit 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
ulimitis 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-1anderrnois 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.
vlimitsets the current limit for a resource for a process.resource identifies the resource:
LIM_CPU- Maximum CPU time. Same as
RLIMIT_CPUforsetrlimit.LIM_FSIZE- Maximum file size. Same as
RLIMIT_FSIZEforsetrlimit.LIM_DATA- Maximum data memory. Same as
RLIMIT_DATAforsetrlimit.LIM_STACK- Maximum stack size. Same as
RLIMIT_STACKforsetrlimit.LIM_CORE- Maximum core file size. Same as
RLIMIT_CORforsetrlimit.LIM_MAXRSS- Maximum physical memory. Same as
RLIMIT_RSSforsetrlimit.The return value is zero for success, and
-1witherrnoset accordingly for failure:
EPERM- The process tried to set its current limit beyond its maximum limit.