This is procres/rusage_wait.c, an example to accompany the book, The Linux Programming Interface.

This file is not printed in the book; it is the solution to Exercise 36-1 (page 765).

The source code file is copyright 2022, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.


Download procres/rusage_wait.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* rusage_wait.c

   Show that getrusage() RUSAGE_CHILDREN retrieves information
   only about children that have been waited on.
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <sys/resource.h>
#include "tlpi_hdr.h"

#define NSECS 3         /* Amount of CPU to consume in child */

#define SIG SIGUSR1     /* Child uses this signal to tell parent
                           that it is about to terminate */
static void
handler(int sig)
    /* Do nothing: just interrupt sigsuspend() */
static void
printChildRusage(const char *msg)
    struct rusage ru;

    printf("%s", msg);
    if (getrusage(RUSAGE_CHILDREN, &ru) == -1)
    printf("user CPU=%.2f secs; system CPU=%.2f secs\n",
            ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1000000.0,
            ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1000000.0);
main(int argc, char *argv[])
    setbuf(stdout, NULL);       /* Disable buffering of stdout */

    struct sigaction sa;
    sa.sa_handler = handler;
    sa.sa_flags = 0;
    if (sigaction(SIG, &sa, NULL) == -1)

    /* Child informs parent of impending termination using a signal;
       block that signal until the parent is ready to catch it. */

    sigset_t mask;
    sigaddset(&mask, SIG);
    if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)

    switch (fork()) {
    case -1:

    case 0:             /* Child */
        for (clock_t start = clock(); clock() - start < NSECS * CLOCKS_PER_SEC;)
            continue;           /* Burn NSECS seconds of CPU time */
        printf("Child terminating\n");

        /* Tell parent we're nearly done */

        if (kill(getppid(), SIG) == -1)

    default:    /* Parent */
        sigsuspend(&mask);      /* Wait for signal from child */

        sleep(2);               /* Allow child a bit more time to terminate */

        printChildRusage("Before wait: ");
        if (wait(NULL) == -1)
        printChildRusage("After wait:  ");


Download procres/rusage_wait.c

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1