Skip to content
This repository
Newer
Older
100644 146 lines (120 sloc) 3.3 kb
67729682 »
2011-02-13 add forkbomb tester
1 /*
2 Copyright 2010,2011 ulatencyd developers
3
4 This file is part of ulatencyd.
5
6 ulatencyd is free software: you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the
8 Free Software Foundation, either version 3 of the License,
9 or (at your option) any later version.
10
11 ulatencyd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ulatencyd. If not, see http://www.gnu.org/licenses/.
18 */
19
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 int
32 main (argc, argv)
33 int argc;
34 char **argv;
35 {
36 int c = 0;
37 int i = 0;
38 int nums = 2000;
39 int delay = 50000;
40 int work = 0;
41 int rand = open("/dev/urandom",O_RDONLY);
42 int data[2];
43 int tmpi;
44 pid_t pid;
45
46
47 while (1)
48 {
49 int option_index = 0;
50 static struct option long_options[] =
51 {
52 {"nums", 1, 0, 'n'},
53 {"delay", 1, 0, 'd'},
54 {"work", 0, 0, 'w'},
55 {"help", 0, 0, 'h'},
56 {0, 0, 0, 0}
57 };
58
59 c = getopt_long (argc, argv, "n:c:hd:",
60 long_options, &option_index);
61 if (c == -1)
62 break;
63
64 switch (c)
65 {
66 case 0:
67 printf ("option %s", long_options[option_index].name);
68 if (optarg)
69 printf (" with arg %s", optarg);
70 printf ("\n");
71 break;
72
73 case 'h':
74 printf ("forkbomb:\n");
75 printf ("-n num number of forks to create\n");
76 printf ("-d delay delay in usecs between alloc \n");
77 exit(0);
78 break;
79
80 case 'n':
81 nums = atoi(optarg);
82 break;
83
84 case 'w':
85 work = 1;
86 break;
87
88 case 'd':
89 delay = atoi(optarg);
90 break;
91
92
93 case '?':
94 break;
95
96 default:
97 printf ("?? getopt returned character code 0%o ??\n", c);
98 }
99 }
100
101 if (optind < argc)
102 {
103 printf ("non-option ARGV-elements: ");
104 while (optind < argc)
105 printf ("%s ", argv[optind++]);
106 printf ("\n");
107 }
108
109 printf(
110 "!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!\n"
111 "!!!!! THIS PROGRAM WILL LIKELY KILL YOUR COMPUTER !!!!!\n"
112 "!!!!! ulatency may rescue you ;-) !!!!!\n"
113 "!!!!! press ctrl+c to stop !!!!!\n"
114 "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
115 );
116
117 sleep(3);
118
119
120 printf ("fork %d childs %s (delay %d us)e:\n", nums, work ? "working" : "", delay);
121
122 while(1) {
123 if(i < nums) {
124 pid = fork();
125 if(pid < 0) {
126 printf("can't fork\n");
127 }
128 if(pid == 0) {
129 printf(".");
130 fflush(stdout);
131 if(work) {
132 read(rand, &data, sizeof(int)*2);
133 if(data[1] != 0) {
134 tmpi = data[0]/data[1];
135 }
136 } else {
137 sleep(10000);
138 }
139 }
140 i++;
141 }
142 usleep(delay);
143 }
144
145 exit (0);
146 }
Something went wrong with that request. Please try again.