0
#include <stdio.h>
#include <sys/type.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(void)
{
    pid_t pid;
    int i;
    for(i=0; i<3; i++) {
        pid = fork();
        if(pid == -1) {
            printf("Fork Error.\n");
        } else if(pid == 0) {
            printf("I am child");
        }
    }

    if(pid != 0) {
        while((pid = waitpid(-1, NULL, 0)) > 0)
            if(errno == ECHILD)
                break;
            printf("I am parent and all children have exited.\n");
    }
    exit(0);

    return 0;
}

The result is that,

'I am child' is printed 7 times, 'I am parent and all children have exited.' is printed 4 times

and the print sequence is not fixed.

I figured out some of the questions that I have been struggling with, but some are not. So, here it is.

Why 'I am parent and all children have exited.' is printed 4 times?

Could you explain it in detail?

PS. I know that sys/wait.h needs to be inserted on top of the code, but my boss doesn't want me to do so.

thsamajisama
  • 51
  • 1
  • 6
  • Does this answer your question? [Visually what happens to fork() in a For Loop](https://stackoverflow.com/questions/26793402/visually-what-happens-to-fork-in-a-for-loop) – Francesco May 07 '20 at 13:49
  • I've seen it before, but it doesn't really help me. If you can answer my question, could you explain it in detail? – thsamajisama May 07 '20 at 14:17
  • Does this answer your question? [fork() in a For Loop](https://stackoverflow.com/questions/61649564/fork-in-a-for-loop) – user12986714 May 07 '20 at 14:45

1 Answers1

0

You fork inside a loop, and basically your child processes continue to execute the loop inside which they started. So on the next iteration they might reach a fork and create their own child.

If you insert a break; right after printf("I am child"); , exiting the loop when you just created a child, and letting the parent continue to create more child processes, you probably will get something closer to what you expected.

Julien Thierry
  • 651
  • 4
  • 11
  • Could you explain why the number of times of executed printf("I am parent ~"); is 4 in detail? – thsamajisama May 07 '20 at 13:59
  • First parent is the main process. It creates children with i = 0, i = 1, i = 2. (lets call them child[i]). child[0] and child[1] will both create children (child[2] is on the last iteration of the loop). child[0] will create two children, child[1] will create one. So they both can wait on a child (and will print that they are parents). Then the first child created by the child[0] will also create a child.And that's your fourth parent. – Julien Thierry May 07 '20 at 14:07
  • I don't understand why child[0] and child[1] both create children at the first time... – thsamajisama May 07 '20 at 14:18
  • When you create a child process with fork(), the child process starts executing from right after the fork. If you forked inside a loop, the child process will continue executing the loop. And since your loop contains a fork(), you child process might reach the fork() and create a child itself. Is that clearer? – Julien Thierry May 07 '20 at 14:31
  • I know your point. What I thought was, if i = 0, child[0] is created and printf("I am child\n"); is executed if i = 1, child[1] and child[2] are created from child[0] and printf("I am child\n"); is executed twice. if i = 2, child[3] and child[4] are created from child[1], and child[5] and child[6] are created from child[2], and printf("I am child\n"); is executed 4times. So the total number of times that printf("I am child\n"); is executed is 1 + 2 + 4 = 7 times. But I don't understand why printf("I am parent ~"); is executed 4 times. Could you explain it in detail please? (I'm sorry) – thsamajisama May 07 '20 at 14:55