Chapter 36. Process Resources

Each process consumes system resources such as memory and CPU time. This chapter looks at resource-related system calls. We begin with the getrusage() system call, which allows a process to monitor the resources that it has used or that its children have used. We then look at the setrlimit() and getrlimit() system calls, which can be used to change and retrieve limits on the calling process’s consumption of various resources.

The getrusage() system call retrieves statistics about various system resources used by the calling process or by all of its children.

#include <sys/resource.h>

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

Note

Returns 0 on success, or -1 on error

The who argument specifies the process(es) for which resource usage information is to be retrieved. It has one of the following values:

RUSAGE_SELF

Return information about the calling process.

RUSAGE_CHILDREN

Return information about all children of the calling process that have terminated and been waited for.

RUSAGE_THREAD (since Linux 2.6.26)

Return information about the calling thread. This value is Linux-specific.

The res_usage argument is a pointer to a structure of type rusage, defined as shown in Example 36-1.

As indicated in the comments in Example 36-1, on Linux, many of the fields in the rusage structure are not filled in by getrusage() (or wait3() and wait4()), or they are filled in only by more recent kernel versions. Some of the fields that are unused on Linux are used on other UNIX implementations. These fields are provided on Linux so that, if they are implemented at a future date, the rusage structure does not need to undergo a change that would break existing application binaries.

The ru_utime and ru_stime fields are structures of type timeval (Calendar Time), which return the number of seconds and microseconds of CPU time consumed by a process in user mode and kernel mode, respectively. (Similar information is retrieved by the times() system call described in Section 10.7.)

The rusage structure returned by the getrusage() RUSAGE_CHILDREN operation includes the resource usage statistics of all of the descendants of the calling process. For example, if we have three processes related as parent, child, and grandchild, then, when the child does a wait() on the grandchild, the resource usage values of the grandchild are added to the child’s RUSAGE_CHILDREN values; when the parent performs a wait() for the child, the resource usage values of both the child and the grandchild are added to the parent’s RUSAGE_CHILDREN values. Conversely, if the child does not wait() on the grandchild, then the grandchild’s resource usages are not recorded in the RUSAGE_CHILDREN values of the parent.

For the RUSAGE_CHILDREN operation, the ru_maxrss field returns the maximum resident set size among all of the descendants of the calling process (rather than a sum for all descendants).

Note

SUSv3 specifies that if SIGCHLD is being ignored (so that children are not turned into zombies that can be waited on), then the child statistics should not be added to the values returned by RUSAGE_CHILDREN. However, as noted in Ignoring Dead Child Processes, in kernels before 2.6.9, Linux deviates from this requirement—if SIGCHLD is ignored, then the resource usage values for dead children are included in the values returned for RUSAGE_CHILDREN.