• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) International Business Machines  Corp., 2009
2  * Author: Matt Helsley <matthltc@us.ibm.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 
23 #include <sys/time.h>
24 #include <sys/wait.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <signal.h>
28 
main(int argc,char ** argv)29 int main(int argc, char **argv)
30 {
31 	struct timeval start, now;
32 	pid_t child_root;
33 	unsigned int duration = 1, num_tasks = 0;
34 
35 	if (argc > 1) {
36 		int i;
37 
38 		for (i = 0; i < argc; i++) {
39 			if (strcmp(argv[i], "--help") == 0) {
40 				printf("%s [duration]\n", argv[0]);
41 				exit(EXIT_SUCCESS);
42 			}
43 
44 			if (sscanf(argv[i], "%u", &duration) == 1) {
45 				printf("%u second duration\n", duration);
46 				break;
47 			}
48 		}
49 	}
50 
51 	/*
52 	 * Wait until we're told to go. This allows the driver script to
53 	 * configure the test system (e.g. by putting this task in a
54 	 * suitable cgroup) before the timed forkbomb begins.
55 	 */
56 	{
57 		char *word;
58 		while (scanf("%as", &word) < 1) {
59 		}
60 		free(word);
61 	}
62 	if (gettimeofday(&start, NULL))
63 		exit(EXIT_FAILURE);
64 	child_root = fork();
65 	if (child_root == -1)
66 		exit(EXIT_FAILURE);
67 /*	if (child_root == 0)
68 		signal(SIGCHLD, SIG_IGN);*/
69 	do {
70 		if (child_root == 0) {
71 /*			signal(SIGCHLD, SIG_IGN);*/
72 			if (fork() == -1)
73 				break;
74 		} else {
75 			if (wait(NULL) > 0)
76 				num_tasks++;
77 		}
78 		if (gettimeofday(&now, NULL) && (errno != EINTR))
79 			break;
80 	} while ((now.tv_sec - start.tv_sec) <= duration);
81 
82 	if (child_root != 0)
83 		printf("Forked %d tasks\n", num_tasks);
84 	exit(EXIT_SUCCESS);
85 }
86