NAME
getrlimit, setrlimit - Controls maximum system resource con-
sumption
SYNOPSIS
#include <sys/time.h>
#include <sys/resource.h>
int setrlimit(
int resource1,
struct rlimit *rlp );
int getrlimit (
int resource1,
struct rlimit *rlp );
PARAMETERS
resource1 Specifies one of the following values:
RLIMIT_CPU
The maximum amount of CPU time (in
seconds) to be used by each process.
RLIMIT_FSIZE
The largest size, in bytes, of any sin-
gle file that can be created.
RLIMIT_DATA
The maximum size, in bytes, of the data
segment for a process; this defines how
far a program can extend its break with
the sbrk() function.
RLIMIT_NOFILE
The maximum number of open file descrip-
tors that the process can have. Any
functions that create new file descrip-
tors will fail with errno set to
[EMFILE].
RLIMIT_AS The maximum amount of the address space
of a process that is defined (in bytes).
This resource setting may cause mmap()
and malloc() to fail with errno set to
[ENOMEM]. Also, the automatic stack
growth will fail with the effects
described later in this section.
RLIMIT_STACK
The maximum size, in bytes, of the stack
segment for a process; this defines how
far a program stack segment can be
extended. Stack extension is performed
automatically by the system.
RLIMIT_CORE
The largest size, in bytes, of a core
file that can be created.
RLIMIT_RSS
The maximum size, in bytes, to which a
process's resident set size can grow.
This imposes a limit on the amount of
physical memory to be given to a pro-
cess; if memory is tight, the system
prefers to take memory from processes
that are exceeding their declared
resident set size.
rlp Points to the rlimit structure, which contains the
current soft and hard limits. For the getrlimit()
function, the requested limits are returned in
this structure, and for the setrlimit() function,
the desired new limits are specified here.
DESCRIPTION
The getrlimit() function obtains the limits on the consump-
tion of system resources by the current process and each
process it creates. The setrlimit() function is used to set
these resources.
Each resource limit is specified as either a soft limit or a
hard limit. When a soft limit is exceeded (for example, if
the CPU time is exceeded) a process can receive a signal
until it reaches the hard limit, or until it modifies its
resource limit. The rlimit structure is used to specify the
hard and soft limits on a resource, as defined in the
sys/resource.h header file.
The calling process must have superuser privilege in order
to raise the maximum limits. An unprivileged process can
alter the rlim_cur field of the rlimit structure within the
range from 0 (zero) to rlim_max or can (irreversibly) lower
rlim_max.
An infinite value for a limit is defined as RLIM_INFINITY.
Because this information is stored in the per-process infor-
mation, and inherited by fork(), the setrlimit() function
should be executed directly by the shell in order to affect
all future processes created by the shell; limit is thus a
built-in command to the shells.
The system refuses to extend the data or stack space when
the limits would be exceeded in the normal way: a brk()
function fails if the data space limit is reached. When the
stack limit is reached, the process receives a SIGSEGV sig-
nal; if this signal is not caught by a handler using the
signal stack, this signal kills the process. A file I/O
operation that would create a file that is too large causes
a signal SIGXFSZ to be generated; this normally terminates
the process, but can be caught. When the soft CPU time limit
is exceeded, a signal SIGXCPU is sent to the offending pro-
cess.
NOTES
The ulimit() function is implemented in terms of setr-
limit(). Therefore, the two interfaces should not be used
in the same program. The result of doing so is undefined.
RETURN VALUES
Upon successful completion, a value of 0 (zero) is returned.
Otherwise, a value of -1 is returned and errno is set to
indicate the error.
ERRORS
If the getrlimit() or setrlimit() function fails, errno may
be set to one of the following values:
[EFAULT] The address specified for the rlp parameter is
invalid.
[EINVAL] The resource1 parameter is not a valid resource.
[EPERM] The limit specified to the setrlimit() function
would have raised the maximum limit value, and the
caller does not have appropriate privilege.
RELATED INFORMATION
Functions: quota(2), setquota(2), sigaction(2), sigstack(2),
sigvec(2), ulimit(3)
Acknowledgement and Disclaimer