From c63f340d90800895f007de64b7d2d14624263331 Mon Sep 17 00:00:00 2001 From: nthnluu Date: Sun, 28 Jan 2024 21:20:27 -0500 Subject: Created student weenix repository --- user/usr/bin/tests/forkbomb.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 user/usr/bin/tests/forkbomb.c (limited to 'user/usr/bin/tests/forkbomb.c') 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 +#include +#include +#include + +/* 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; +} -- cgit v1.2.3-70-g09d2