cgroups/fork_bomb.c

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

This file is not printed in the book; it demonstrates Linux features that are not described in the book (typically features that have appeared since the book was published).

The source code file is copyright 2024, 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 cgroups/fork_bomb.c

  Cover of The Linux Programming Interface

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

/* fork_bomb.c

   A fork-bomb program that can be useful when experimenting with
   the cgroups 'pids' controller.

   Usage: fork_bomb num-children [child-sleep-secs]

   This program uses fork(2) to create 'num-children' child processes that
   each sleep (i.e., stay in existence) for 'child-sleep-secs' (default:
   300) seconds.

   The program pauses until the user types "Enter" before creating any
   children.  The intention of this pause is to allow the user some time
   to manipulate the cgroup membership of the parent process before any
   child processes are created.
*/
#include <sys/wait.h>
#include "tlpi_hdr.h"
int
main(int argc, char *argv[])
{
    if (argc < 2) {
        usageErr("%s num-children [child-sleep-secs]\n",
                argv[0]);
    }

    int numChildren = atoi(argv[1]);
    int childSleepTime = (argc > 2) ? atoi(argv[2]) : 300;

    printf("Parent PID = %ld\n", (long) getpid());

    printf("Parent pausing; hit ENTER when ready\n");

    getchar();

    printf("Creating %d children that will sleep %d seconds\n",
            numChildren, childSleepTime);

    int failed = 0;
    for (int j = 1; j <= numChildren && !failed; j++) {
        pid_t childPid = fork();
        switch (childPid) {
        case -1:
            errMsg("fork");
            failed = 1;
            break;
        case 0:
            sleep(childSleepTime);
            exit(EXIT_SUCCESS);
        default:
            printf("Child %d: PID = %ld\n", j, (long) childPid);
            break;
        }
    }

    printf("Waiting for all children to terminate\n");

    while (waitpid(-1, NULL, 0) > 0)
        continue;

    printf("All children terminated; bye!\n");

    exit(EXIT_SUCCESS);
}

 

Download cgroups/fork_bomb.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