From 4f2637ffc0e9a4db145f4343445de788777b2a51 Mon Sep 17 00:00:00 2001 From: damage Date: Tue, 27 Aug 2024 20:14:04 +0200 Subject: [PATCH] more loops, higher count, use flush --- fork.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/fork.c b/fork.c index ef19eb9..9220505 100644 --- a/fork.c +++ b/fork.c @@ -3,25 +3,36 @@ #include #include #include +#include + +#define LOOPS_PER_CHILD 2000 +#define CHILDS 30 int main() { - for (int start = 0; start < 50; start += 10) { + for (int start = 0; start < LOOPS_PER_CHILD * CHILDS; start += LOOPS_PER_CHILD) { pid_t pid = fork(); if (pid == -1) { fprintf(stderr, "Error forking: %s\n", strerror(errno)); return 1; } else if (pid == 0) { // I AM FORK! - for (int i = start; i < start + 10; i++) { - printf("%d: %d\n", pid, i); + pid_t childPid = getpid(); + + for (int i = start; i < start + LOOPS_PER_CHILD; i++) { + fprintf(stdout, "%d: %d\n", childPid, i); + fflush(stdout); } - printf("Fork %d finished counting from %d\n", getpid(), start); + printf("Fork %d finished counting from %d\n", childPid, start); return 0; } else { printf("Fork %d successfull\n", pid); } } + // wait for all childs to terminate + while (wait(NULL) > -1); + + return 0; fork(); fork();