How to find resource limits in UNIX using ‘getrlimit( )’ function ?

Every process has a set of resource limits. For example, a process can have some maximum limit on number of file descriptors that can be opened by it; a process has a limit on the maximum amount of CPU time it can use at a time etc. So, it is possible in UNIX to query those resource limit value and also possible to change its limit value by the ‘getrlimit( )’ and ‘setrlimit( )’ APIs. Their prototype is as shown below.

 

#include <sys/time.h>

#include <sys/resource.h>

int getrlimit( int rce, struct rlimit *ptr );

int setrlimit( int rce, const struct rlimit *ptr );

RETURNS: Both returns 0 if successful; -1 on error.

 

The structure associated with these function is

 

struct rlimit 

{

             rlim_t  rlim_cur;  /* soft limit : current limit */

             rlim_t  rlim_max; /* hard limit : max. value for rlim_cur */

};

So the above functions take a single ‘rce’ and a pointer to the above shown structure as its arguments. The rules that govern the changing of the resource limits are as follows:

 

  • A soft limit can be changed by any process to a value less than or equal to its hard limit.
  • Any process can lower its hard limit to a value greater than or equal to its soft limit. But a normal user cannot increase the value of the hard limit.
  • Only a super user process can increase the value of a hard limit.

There are certain resource limits associated with a process like the CPU limit which can be infinite. An infinite limit is specified the constant RLIM_INFINITY.

Let us have a look into the possible ‘rce’ (resource) values used in the ‘setrlimit( )’ and ‘getrlimit( )’ functions. These resource limits are constants as shown below.

RLIMIT_CORE: The maximum size of a core file that can be generated by a process measured in bytes. A limit of 0 prevents that creation of a core file.

RLIMIT_CPU: The maximum amount of CPU time that a process can consume measured in seconds. When the softlimit is exceeded, the SIGXPU signal is sent to the calling process.

RLIMIT_DATA: The maximum size of the data segment that a program execution can have measured in bytes. This maximum size is equal to the sum of the sizes of initialized read only data segment, initialized read-write data segment, uninitialized data segment and heap segment.

RLIMIT_FSIZE: The maximum size of a file that can be created measured in bytes. If the soft limit is exceeded, the SIGXFSZ signal is sent to the calling process.

RLIMIT_NOFILE: The maximum number of open files per process.

RLIMIT_NPROC: The maximum number of child process per real USER ID.

RLIMIT_STACK: The maximum size of the stack during program execution measured in bytes.

RLIMIT_VMEM: The maximum size of the mapped address space measured in bytes. This affects the ‘mmap( )’ function.

Let us now write a simple program to find the soft limit and hard limit for the CPU usage and data segment for any process supported by our system. The program is shown below.

Program – 1:

/* Finds CPU limits */

#include <stdio.h>

#include <sys/types.h>

#include <sys/resource.h>

#include <sys/time.h>

#include <errno.h>

int main( )

{

struct rlimit limit;

if( getrlimit( RLIMIT_CPU, &limit) < 0 )

fprintf( stderr, “%s\n”, strerror(errno));

else

printf( “%-14s ” , ” RLIMIT_CPU” );

if ( limit.rlim_cur == RLIM_INFINITY )

printf( ” (infinity) ” );

else

printf( “%10ld “, limit.rlim_cur );

if( limit.rlim_max == RLIM_INFINITY )

printf( “(infinity) \n” );

else

printf( “10ld\n”, limit.rlim_max );

return 0;

}

 

Ouput:

RLIM_CPU   (infinity) (infinity)

Program – 2

/* Finds data segment limits */

#include <stdio.h>

#include <sys/types.h>

#include <sys/resource.h>

#include <sys/time.h>

#include <errno.h>

int main( )

{

struct rlimit limit;

if( getrlimit( RLIMIT_DATA, &limit ) < 0 )

fprintf( stderr, “%s\n”, strerror(errno));

else

printf( “%-14s “, “RLIMIT_DATA” );

if( limit.rlim_cur == RLIM_INFINITY )

printf( ” (infinity) ” );

else

printf( “%10d ” , limit.rlim_cur );

if( limit.rlim_max == RLIM_INFINITY )

printf( ” ( infinity ) ” );

else

printf( “10Ld\n”, limit.rlim_max );

return 0;

}

Output:

RLIMIT_DATA 8388608   16777216

 

Visit: SUBHASH LOVES TO TEACH ! CLICK HERE !

Author Information:

Subhash.K.U, Principal Mentor, Subhash Programming Classes,Bangalore.

VIEW SUBHASH’s PROFILE

One Response Comment

  • Vinutha.M.V.  March 18, 2016 at 1:51 pm

    Thank you sir…. It is very useful.

    Reply

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.