timers/cpu_burner.c

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

This file is not printed in the book; it is a supplementary file for Chapter 23.

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 timers/cpu_burner.c

  Cover of The Linux Programming Interface

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

/* cpu_burner.c

   A small program that simply consumes CPU time, displaying the amount of
   elapsed time that was required to consume each CPU second.
*/
#include <sys/times.h>
#include <time.h>
#include <signal.h>
#include "tlpi_hdr.h"

static volatile sig_atomic_t gotSig = 0;
static void
handler(int sig)
{
    gotSig = 1;
}
int
main(int argc, char *argv[])
{
    struct timespec curr_cpu;

    struct sigaction sa;
    sa.sa_handler = handler;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);

    if (sigaction(SIGTERM, &sa, NULL) == -1)
        errExit("sigaction");
    if (sigaction(SIGINT, &sa, NULL) == -1)
        errExit("sigaction");

    struct timespec prev_rt;
    if (clock_gettime(CLOCK_REALTIME, &prev_rt) == -1)
        errExit("clock_gettime");

    /* Loop consuming CPU time until we get sent a signal */

    time_t prev_cpu_secs = 0;

    while (!gotSig) {
        if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &curr_cpu) == -1)
            errExit("clock_gettime");

        /* Each time the CPU time clock ticks over to another second,
           display the amount of time that elapsed since consuming the
           previous second of CPU time. */

        if (curr_cpu.tv_sec > prev_cpu_secs) {
            struct timespec curr_rt;
            if (clock_gettime(CLOCK_REALTIME, &curr_rt) == -1)
                errExit("clock_gettime");

            int elapsed_us = (curr_rt.tv_sec - prev_rt.tv_sec) * 1000000 +
                             (curr_rt.tv_nsec - prev_rt.tv_nsec) / 1000;

            printf("[%ld] %ld: elapsed/cpu = %5.3f; %%CPU = %5.3f\n",
                    (long) getpid(),
                    (long) curr_cpu.tv_sec, elapsed_us / 1000000.0,
                    1000000.0 / elapsed_us * 100.0);

            prev_cpu_secs = curr_cpu.tv_sec;
            prev_rt = curr_rt;
        }
    }

    printf("Bye!\n");
    exit(EXIT_SUCCESS);
}

 

Download timers/cpu_burner.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