1
$\begingroup$

If a process executes the following code

fork();
fork();
fork();

The total number of child processes created is given as 7 by a text book. But how does that happen

fork generates 2 processes right? Are both the processes referred to as child processes? One process is the parent parent and one is the child right? Even if both the processes are referred to as child processes,2*3=6? The main process is not a child process,how does the number 7 come into the picture.

Update:

enter image description here

$\endgroup$
  • $\begingroup$ You should use it practically to see how it works. Then it will be easy for you to understand. $\endgroup$ – Deep Joshi Sep 18 '18 at 9:22
  • $\begingroup$ Each fork command generates a new process and the created process starts executing from the subsequent line of code. So in total, seven child processes are created - eight including the original one. $\endgroup$ – Gokul Sep 18 '18 at 9:54
  • $\begingroup$ @Gokul if each fork command generates a single process,There will be a total of 4 processes, right? $\endgroup$ – techno Sep 18 '18 at 9:57
  • $\begingroup$ No, that's not how it works. Check this link. $\endgroup$ – Gokul Sep 18 '18 at 10:10
3
$\begingroup$

Each fork call duplicate process, so after first call there are two of them. You may call the one with pid = 0, the master process (parent, top, original).

After that two processes are in the second line (second fork), executing it, there are four processes now. I have not seen such distinction, but you may at that point say that two of them are child processes.

After the final fork, there are eight processes altogether, according to your materials, the original process is called parent and the rest are childs.

To draw it properly you should create a tree, with two children after each fork, where in fact one of them is the original caller.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.