getrusage()

Updated: April 19, 2023

Get information about resource utilization

Synopsis:

#include <sys/resource.h>

int getrusage( int who,
               struct rusage * r_usage );

Arguments:

who
Which process to get the usage for:
  • RUSAGE_CHILDREN — get information about resources used by the terminated and waited-for children of the current process. If the child is never waited for (e.g., if the parent has SA_NOCLDWAIT set, or sets SIGCHLD to SIG_IGN), the resource information for the child process is discarded and isn't included.
  • RUSAGE_SELF — get information about resources used by the current process.
r_usage
A pointer to an object of type struct rusage in which the function can store the resource usage information; see below.

Library:

libc

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

Description:

The getrusage() function provides measures of the resources used by the current process or its terminated and waited-for child processes, depending on the value of the who argument.

There's no way to obtain information about a child process that hasn't yet terminated.

The rusage structure includes at least the following two fields:

struct rusage {
   struct timeval  ru_utime;    /* user time used */
   struct timeval  ru_stime;    /* system time used */
   ...
}

There are also __rusage32 and __rusage64 versions of this structure that are specifically for 32- and 64-bit architectures.

The members shown above are the only ones supported by this function. They are:

ru_utime
The total amount of time, in seconds and microseconds, spent executing in user mode.
ru_stime
The total amount of time, in seconds and microseconds, spent executing in system mode.

This structure has other members, but getrusage() doesn't update them and instead sets them all to 0.

Returns:

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

Errors:

EFAULT
The address specified by the r_usage argument isn't in a valid portion of the process's address space.
EINVAL
Invalid who parameter.

Classification:

POSIX 1003.1

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