Updated: April 19, 2023 |
Get the limit on a system resource
#include <sys/resource.h> int getrlimit( int resource, struct rlimit * rlp ); int getrlimit64( int resource, struct rlimit64 * rlp );
For descriptions and the actions taken when the current limit is exceeded, see prlimit().
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.
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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:
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.
#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; }
getrlimit() is POSIX 1003.1; getrlimit64() is Large-file support
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |