From 8f176fdac8d0ef0f994fdd131395cbc1b9f1b6ec Mon Sep 17 00:00:00 2001 From: andreastaliad Date: Mon, 25 May 2026 15:38:39 +0300 Subject: [PATCH] made parent kill childeren --- fork_bomb/fork_bomb.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/fork_bomb/fork_bomb.c b/fork_bomb/fork_bomb.c index 579294c..84a8e04 100644 --- a/fork_bomb/fork_bomb.c +++ b/fork_bomb/fork_bomb.c @@ -7,7 +7,6 @@ #include #include #include -#include #define DEFAULT_NUM_LEN 512 @@ -202,6 +201,36 @@ static long read_self_ticks(void) { return utime + stime; } +static void kill_children_of(pid_t ppid) { + DIR *dir = opendir("/proc"); + if (!dir) { + return; + } + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + if (ent->d_type != DT_DIR || !is_numeric_dir(ent->d_name)) { + continue; + } + char path[256]; + snprintf(path, sizeof(path), "/proc/%s/stat", ent->d_name); + FILE *f = fopen(path, "r"); + if (!f) { + continue; + } + long pid = 0; + long parent = 0; + char comm[256]; + char state = 0; + if (fscanf(f, "%ld (%255[^)]) %c %ld", &pid, comm, &state, &parent) == 4) { + if (parent == (long)ppid) { + kill((pid_t)pid, SIGKILL); + } + } + fclose(f); + } + closedir(dir); +} + static void read_cpu_total(long *total, long *idle) { FILE *f = fopen("/proc/stat", "r"); if (!f) { @@ -289,12 +318,6 @@ int main(void) } if (pid == 0) { - if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0) { - _exit(1); - } - if (getppid() == 1) { - _exit(0); - } pause(); return 0; } @@ -359,6 +382,8 @@ int main(void) } } + kill_children_of(getpid()); + fclose(csv); return 0;