-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork_bomb.c
83 lines (66 loc) · 2.75 KB
/
fork_bomb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2022. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Supplementary program for Chapter Z */
/* 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);
}