Go to the first, previous, next, last section, table of contents.


Limiting Resource Usage

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 limit. Each process initially inherits its limit values from its parent, but it can subsequently change them.

The symbols in this section are defined in `sys/resource.h'.

Function: int getrlimit (int resource, struct rlimit *rlp)
Read the current value and the maximum value of resource resource and store them in *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 bits system this function is in fact getrlimit64. I.e., the LFS interface transparently replaces the old interface.

Function: int getrlimit64 (int resource, struct rlimit64 *rlp)
This function is similar to the getrlimit but its second parameter is a pointer to a variable of type struct rlimit64 which allows this function 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 bits machine this function is available under the name getrlimit and so transparently replaces the old interface.

Function: int setrlimit (int resource, const struct rlimit *rlp)
Store the current value and the maximum value of resource resource in *rlp.

The return value is 0 on success and -1 on failure. The following errno error condition is possible:

EPERM
You tried to change the maximum permissible limit value, but you don't have privileges to do so.

When the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits system this function is in fact setrlimit64. I.e., the LFS interface transparently replaces the old interface.

Function: int setrlimit64 (int resource, const struct rlimit64 *rlp)
This function is similar to the setrlimit but its second parameter is a pointer to a variable of type struct rlimit64 which allows this function 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 bits machine this function is available under the name setrlimit and so transparently replaces the old interface.

Data Type: struct rlimit
This structure is used with getrlimit to receive limit values, and with setrlimit to specify limit values. It has two fields:

rlim_t rlim_cur
The current value of the limit in question. This is also called the "soft limit".
rlim_t rlim_max
The maximum permissible value of the limit in question. You cannot set the current value of the limit to a larger number than this maximum. Only the super user can change the maximum permissible value. This is also called the "hard limit".

In getrlimit, the structure is an output; it receives the current values. In setrlimit, it specifies the new values.

For the LFS functions a similar type is defined in `sys/resource.h'.

Data Type: struct rlimit64
This structure is used with getrlimit64 to receive limit values, and with setrlimit64 to specify limit values. It has two fields:

rlim64_t rlim_cur
The current value of the limit in question. This is also called the "soft limit".
rlim64_t rlim_max
The maximum permissible value of the limit in question. You cannot set the current value of the limit to a larger number than this maximum. Only the super user can change the maximum permissible value. This is also called the "hard limit".

In getrlimit64, the structure is an output; it receives the current values. In setrlimit64, it specifies the new values.

Here is a list of resources that you can specify a limit for. Those that are sizes are measured in bytes.

RLIMIT_CPU
The maximum amount of cpu time the process can use. If it runs for longer than this, it gets a signal: SIGXCPU. The value is measured in seconds. See section Operation Error Signals.
RLIMIT_FSIZE
The maximum size of file the process can create. Trying to write a larger file causes a signal: SIGXFSZ. See section Operation Error Signals.
RLIMIT_DATA
The maximum size of data memory for the process. If the process tries to allocate data memory beyond this amount, the allocation function fails.
RLIMIT_STACK
The maximum stack size for the process. If the process tries to extend its stack past this size, it gets a SIGSEGV signal. See section Program Error Signals.
RLIMIT_CORE
The maximum size core file that this process can create. If the process terminates and would dump a core file larger than this maximum size, then no core file is created. So setting this limit to zero prevents core files from ever being created.
RLIMIT_RSS
The maximum amount of physical memory that this process should get. This parameter is a guide for the system's scheduler and memory allocator; the system may give the process more memory when there is a surplus.
RLIMIT_MEMLOCK
The maximum amount of memory that can be locked into physical memory (so it will never be paged out).
RLIMIT_NPROC
The maximum number of processes that can be created with the same user ID. If you have reached the limit for your user ID, fork will fail with EAGAIN. See section Creating a Process.
RLIMIT_NOFILE
RLIMIT_OFILE
The maximum number of files that the process can open. If it tries to open more files than this, it gets error code EMFILE. See section Error Codes. Not all systems support this limit; GNU does, and 4.4 BSD does.
RLIM_NLIMITS
The number of different resource limits. Any valid resource operand must be less than RLIM_NLIMITS.

Constant: int RLIM_INFINITY
This constant stands for a value of "infinity" when supplied as the limit value in setrlimit.

Two historical functions for setting resource limits, ulimit and vlimit, are not documented here. The latter is declared in `sys/vlimit.h' and comes from BSD.


Go to the first, previous, next, last section, table of contents.