fork()
splits the current program, making two identical copies - the difference is only in the return code of the fork()
.
So if you fork()
twice:
fork();
fork();
Then what will happen is - the first fork will split parent
into parent + child
.
The second fork will split both, because the child carries on from the same point. Giving you:
parent
+ - child
+ - child
+ - child
To avoid this, you need to check the return code of fork()
and use the return code to decide if you're still in the parent
. It's not necessarily a bad thing, but you need to be aware that it'll happen, and ensure you handle e.g. signals, return codes, waitpids etc. appropriately. So normally you'll do the fork
ing from just the parent.