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 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 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 [parent-sleep-secs [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:
   1000) seconds.

   The 'parent-sleep-secs' argument specifies an a number of seconds
   (default: 0) that the program should sleep before creating any children.
   The intention of this sleep 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 [parent-sleep-secs [child-sleep-secs]]\n",
                argv[0]);
    }

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

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

    /* If a parent sleep time was specified, show the user the parent's
       PID in case they want to write it to a cgroup.procs file */

    if (parentSleepTime > 0) {
        printf("Parent sleeping for %d seconds\n", parentSleepTime);
        sleep(parentSleepTime);
    }

    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