getrlimit(), getrlimit64()

Get the limit on a system resource

Synopsis:

#include <sys/resource.h>

int getrlimit( int resource,
               struct rlimit * rlp );

int getrlimit64( int resource,
                 struct rlimit64 * rlp );

Arguments:

resource
The resource whose limit you want to get; one of the following:

For descriptions and the actions taken when the current limit is exceeded, see setrlimit().

rlp
A pointer to a rlimit or rlimit64 structure where the function can store the limit on the resource. The rlimit and rlimit64 structures include at least the following members:
rlim_t rlim_cur; /* current (soft) limit */
rlim_t rlim_max; /* hard limit */
  

The rlim_t type is an arithmetic data type to which you can cast objects of type int, size_t, and off_t without loss of information.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The getrlimit() and getrlimit64() functions get the limits on the consumption of a variety of system resources by a process and each process it creates. The getrlimit64() function is a large-file support version of getrlimit().

Each call to getrlimit() identifies a specific resource to be operated upon as well as a resource limit. A resource limit is a pair of values:

A process can change soft limits to any value that's less than or equal to the hard limit. A process may (irreversibly) lower its hard limit to any value that's greater than or equal to the soft limit. Only a process with an effective user ID of root can raise a hard limit. Both hard and soft limits can be changed in a single call to setrlimit() subject to the constraints described above. Limits may have an “infinite” value of RLIM_INFINITY.

Because limit information is stored in the per-process information, the shell builtin ulimit command (see the entry for ksh in the Utilities Reference) must directly execute this system call if it's to affect all future processes created by the shell.

The values of the current limit of the following resources affect these parameters:

Resource Parameter
RLIMIT_NOFILE OPEN_MAX

If a resource limit can be represented correctly in an object of type rlim_t, then its representation is returned; otherwise, if the value of the resource limit is equal to that of the corresponding saved hard limit, the value returned is RLIM_SAVED_MAX; otherwise, the value returned is RLIM_SAVED_CUR.

A limit whose value is greater than RLIM_INFINITY is permitted.

The exec* family of functions also causes resource limits to be saved.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EFAULT
The rlp argument points to an illegal address.
EINVAL
An invalid resource was specified.
EPERM
The limit specified to setrlimit() would have raised the maximum limit value, and the effective user of the calling process isn't the superuser.

Examples:

#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>

int main( void )
{
    struct rlimit curr_limits;
    
    if (getrlimit (RLIMIT_NPROC, &curr_limits) == -1) {
        perror ("The call to getrlimit() failed.");
        return EXIT_FAILURE;
    } else {
        printf ("The current maximum number of processes is %d.\n",
               (int) curr_limits.rlim_cur);
        printf ("The hard limit on the number of processes is %d.\n",
               (int) curr_limits.rlim_max);
    }
        return EXIT_SUCCESS;
}

Classification:

getrlimit() is POSIX 1003.1 XSI; getrlimit64() is Large-file support

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fork(), getdtablesize(), malloc(), open(), setrlimit(), setrlimit64(), signal(), sysconf()

ulimit builtin command (see the entry for ksh in the Utilities Reference)