aboutsummaryrefslogtreecommitdiff
path: root/user/usr/bin/tests/forkbomb.c
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /user/usr/bin/tests/forkbomb.c
Created student weenix repository
Diffstat (limited to 'user/usr/bin/tests/forkbomb.c')
-rw-r--r--user/usr/bin/tests/forkbomb.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/user/usr/bin/tests/forkbomb.c b/user/usr/bin/tests/forkbomb.c
new file mode 100644
index 0000000..543990c
--- /dev/null
+++ b/user/usr/bin/tests/forkbomb.c
@@ -0,0 +1,84 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/* TODO add options for different ways of forkbombing
+ (kind of low priority but would be fun) */
+
+int main(int argc, char **argv)
+{
+ int n = 1;
+ pid_t pid;
+
+ open("/dev/tty0", O_RDONLY, 0);
+ open("/dev/tty0", O_WRONLY, 0);
+ printf("Forking up a storm!\n");
+ printf("If this runs for 10 minutes without crashing, then you ");
+ printf("probably aren't \nleaking resources\n");
+
+ if (fork())
+ {
+ for (;;)
+ {
+ printf("Fork number : %d\n", n);
+ if ((pid = fork()))
+ {
+ if (pid == -1)
+ {
+ printf("Fork %d failed. Forkbomb stopping.\n", n);
+ exit(1);
+ }
+ int status;
+ sched_yield();
+ wait(&status);
+ if (status != 0)
+ {
+ printf("Test failed. Child exit with status %d\n", status);
+ exit(1);
+ }
+ }
+ else
+ {
+ int a = 0;
+ sched_yield();
+ exit(0);
+ }
+ n++;
+ }
+ }
+
+#if 0
+// Old forkbomb
+ if (!fork())
+ {
+ for (;;)
+ {
+ printf("I am fork number %d\n", n);
+ if ((pid = fork()))
+ {
+ if (-1 != pid)
+ {
+ exit(0);
+ }
+ else
+ {
+ printf(
+ "%d-th fork failed. "
+ "forkbomb stopping.",
+ n);
+ exit(1);
+ }
+ }
+ ++n;
+ }
+ }
+ else
+ {
+ int status;
+ while (wait(&status) > 0)
+ ;
+ }
+#endif
+ return 0;
+}