getrlimit(), getrlimit64()

Updated: April 19, 2023

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:
  • RLIMIT_AS
  • RLIMIT_CORE
  • RLIMIT_CPU
  • RLIMIT_DATA
  • RLIMIT_FREEMEM
  • RLIMIT_NOCONN_NP
  • RLIMIT_NOFILE
  • RLIMIT_NPROC
  • RLIMIT_NTHR
  • RLIMIT_OFILE
  • RLIMIT_RSS
  • RLIMIT_SIGEVENT_NP
  • RLIMIT_STACK
  • RLIMIT_TIMERS_NP
  • RLIMIT_VMEM

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

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().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in What's in a Function Description?

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:

The call to getrlimit() in turn calls prlimit() with the new_rlp argument set to NULL.

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 the PROCMGR_AID_RLIMIT ability enabled (see procmgr_ability()) 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.

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; getrlimit64() is Large-file support

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